Flutter使用Scaffold报错。

简介: Flutter使用Scaffold报错。

错误信息

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!'),
              ));
            },
          ),
        );
      },
    ),
  );
}

相关文章
|
3月前
|
缓存 Dart 开发工具
解决Flutter报错The method ‘File.create‘ has fewer named arguments than those of overridden method
解决Flutter报错The method ‘File.create‘ has fewer named arguments than those of overridden method
61 3
|
15天前
|
存储 测试技术 Shell
Flutter UT太多导致跑覆盖率报错
Flutter UT太多导致跑覆盖率报错
25 2
|
3月前
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
Flutter更改主题颜色报错:type ‘Color‘ is not a subtype of type ‘MaterialColor‘
44 4
|
3月前
|
开发工具 iOS开发
解决Flutter运行报错Could not run build/ios/iphoneos/Runner.app
解决Flutter运行报错Could not run build/ios/iphoneos/Runner.app
145 2
|
3月前
解决Flutter报错The named parameter |method ‘xxxx‘ isn‘t defined.
解决Flutter报错The named parameter |method ‘xxxx‘ isn‘t defined.
132 3
|
3月前
|
iOS开发
解决Flutter运行IOS报错:Podfile is out of date
解决Flutter运行IOS报错:Podfile is out of date
79 1
|
3月前
解决Flutter报错boxconstraints has non-normalized height/width constraints
解决Flutter报错boxconstraints has non-normalized height/width constraints
44 0
|
3月前
|
Android开发
解决Android、Flutter编译时Gradle报错:javax.net.ssl.SSLException: Connection reset
解决Android、Flutter编译时Gradle报错:javax.net.ssl.SSLException: Connection reset
378 0
|
API Android开发
Flutter导入第三方包后报错The number of method references in a .dex file cannot exceed 64K
Flutter导入第三方包后报错The number of method references in a .dex file cannot exceed 64K
Flutter的setState的使用注意事项以及报错The method ‘setState‘ isn‘t defined for the type
Flutter的setState的使用注意事项以及报错The method ‘setState‘ isn‘t defined for the type