1.问题
flutter在使用新版本时,遇见
Don't use 'BuildContext's across async gaps.\nTry rewriting the code to not use the 'BuildContext', or guard the use with a 'mounted' check
的问题。
这意味着在异步操作之后不应该再使用 BuildContext
因为提供该上下文的widget可能已经不在widget树中。如果你尝试访问它的上下文或者在其中进行路由,可能会导致潜在的错误或者崩溃。
2.解决方案
检查mounted的属性
对于Widget而言
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
void _onButtonPressed(BuildContext context) async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const DetailPage(
data: 'Hello from PageOne',
)));
if (!context.mounted) {
return;
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('receive result:$result')),
);
}
|
文章作者
梵梵爸
上次更新
2024-03-05
许可协议
原创文章,如需转载请注明文章作者和出处。谢谢