错误信息
E/flutter ( 7426): [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 ( 7426): 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 ( 7426): 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 ( 7426): https://docs.flutter.io/flutter/material/Scaffold/of.html
E/flutter ( 7426): 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 ( 7426): 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 ( 7426): The context used was:
E/flutter ( 7426): Home(state: _HomeState#497fc)
E/flutter ( 7426): #0 Scaffold.of (package:flutter/src/material/scaffold.dart:1156:5)
E/flutter ( 7426): #1 _HomeState.build.. (package:my_app/home_widget.dart:46:28)
E/flutter ( 7426): #2 State.setState (package:flutter/src/widgets/framework.dart:1122:30)
E/flutter ( 7426): #3 _HomeState.build. (package:my_app/home_widget.dart:45:17)
E/flutter ( 7426): #4 _PopupMenuButtonState.showButtonMenu. (package:flutter/src/material/popup_menu.dart)
E/flutter ( 7426): #5 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 7426): #6 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 7426): #7 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 7426): #8 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 7426): #9 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 7426): #10 Future._completeWithValue (dart:async/future_impl.dart:483:5)
E/flutter ( 7426): #11 Future._asyncComplete. (dart:async/future_impl.dart:513:7)
E/flutter ( 7426): #12 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 7426): #13 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 7426): #14 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 7426): #15 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter ( 7426): #16 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7426): #17 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
解决方案
使用Builder来包装
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Demo') ), body: Builder( // Create an inner BuildContext so that the onPressed methods // can refer to the Scaffold with Scaffold.of(). builder: (BuildContext context) { return Center( child: RaisedButton( child: Text('SHOW A SNACKBAR'), onPressed: () { Scaffold.of(context).showSnackBar(SnackBar( content: Text('Hello!'), )); }, ), ); }, ), ); }