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);
}
.......
完美解决。原因大概是未找到当前上下文,有待商戳。