final helpers = Helpers();
start() async {
try {
if (energy == 0) {
if (!mounted) return;
helpers.showSnackWithKey(widget.scaffoldKey, "Not enough energy");
showRemaining = true;
return;
}
return Card(
shadowColor: Colors.blue,
child: Column(
children: [
...
ElevatedButton(
onPressed: () {
start();
},
...
-helpers.dart
showSnackWithKey(scaffoldKey, message) {
print(message);
ScaffoldMessenger.of(scaffoldKey.currentContext).showSnackBar(
SnackBar(
backgroundColor: Colors.blueGrey,
behavior: SnackBarBehavior.floating,
elevation: 3,
content: Text(message),
),
);
}
suse null断言。而不是在助手中,使用
ScaffoldMessenger.of(scaffoldKey.currentContext)
ScaffoldMessenger.of(scaffoldKey.currentContext!)
GlobalKey.currentContext
是
ScaffoldMessenger.of
类型,因此没有IDE警告。
Edit:我遇到了一次无效的错误,但无法复制。我尝试运行提供代码,它可以显示为预期的消息
scaffoldKey
您可以在Flutter中使用另一_Flushbar包。该软件包有用,有创意且可自定义。我最近使用了它,我推荐给您。您可以使用此软件包而不是Snackbar。
dynamic
You can find the bellow link :
https://pub.dev/packages/another_flushbar