Flutter showSnackBar 报错:

Flutter 使用Scaffold展示SnackBar报错:

源代码:

Scaffold.of(context).showSnackBar(new SnackBar(
            duration: Duration(seconds: 2),
            content: new Text("this is snackBar"),
          ));

报错:

Performing hot restart...
Syncing files to device STF AL10...
Restarted application in 2,335ms.
E/flutter ( 5796): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold.
E/flutter ( 5796): No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). This usually happens when the context provided is from the same StatefulWidget as that whose build function actually creates the Scaffold widget being sought.
E/flutter ( 5796): There are several ways to avoid this problem. The simplest is to use a Builder to get a context that is "under" the Scaffold. For an example of this, please see the documentation for Scaffold.of():
E/flutter ( 5796):   https://api.flutter.dev/flutter/material/Scaffold/of.html
E/flutter ( 5796): A more efficient solution is to split your build function into several widgets. This introduces a new context from which you can obtain the Scaffold. In this solution, you would have an outer widget that creates the Scaffold populated by instances of your new inner widgets, and then in these inner widgets you would use Scaffold.of().
E/flutter ( 5796): A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, then use the key.currentState property to obtain the ScaffoldState rather than using the Scaffold.of() function.
E/flutter ( 5796): The context used was:
E/flutter ( 5796):   HomePage(state: HomePageState#1a592)
E/flutter ( 5796): #0      Scaffold.of (package:flutter/src/material/scaffold.dart:1250:5)
E/flutter ( 5796): #1      HomePageState.createPopMenus.<anonymous closure> (package:flutterdemo/widgets/home_page.dart:202:20)
E/flutter ( 5796): #2      _PopupMenuButtonState.showButtonMenu.<anonymous closure> (package:flutter/src/material/popup_menu.dart)
E/flutter ( 5796): #3      _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 5796): #4      _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 5796): #5      _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 5796): #6      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 5796): #7      Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 5796): #8      Future._completeWithValue (dart:async/future_impl.dart:483:5)
E/flutter ( 5796): #9      Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:513:7)
E/flutter ( 5796): #10     _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 5796): #11     _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 5796): #12     _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 5796): #13     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
E/flutter ( 5796): #14     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 5796): #15     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter ( 5796): 

解决方案 给它一个GlobalKey:

 var _scaffoldkey = new GlobalKey<ScaffoldState>();

   return new Scaffold(
      key: _scaffoldkey,
      ........
      ),
      .......
      onPressed: () {
     var snackBar = SnackBar(content: Text('显示snackBar'));
     _scaffoldkey.currentState.showSnackBar(snackBar);
              }
       .......

完美解决。原因大概是未找到当前上下文,有待商戳。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值