int*_*syo 5 dart snackbar flutter
请帮助解释为什么有时我的 SnackBar 显示错误。我不明白在什么情况下会出现这个问题。我还想指出,即使它显示错误,但在应用程序本身中,它工作正常。
这是我的代码:
void showFailedSnackBar(String s) {
SnackBar snackBar = SnackBar(
content: Text(s),
duration: Duration(seconds: 3),
backgroundColor: Theme.of(context).primaryColor,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
E/flutter ( 7879): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
E/flutter ( 7879): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
Run Code Online (Sandbox Code Playgroud)
尝试检查小部件是否仍然安装在三个中:
void showFailedSnackBar(String s) {
if (mounted) {
SnackBar snackBar = SnackBar(
content: Text(s),
duration: Duration(seconds: 3),
backgroundColor: Theme.of(context).primaryColor,
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
}
Run Code Online (Sandbox Code Playgroud)