简体   繁体   English

未处理的异常:使用不包含 Scaffold 的上下文调用 Scaffold.of()

[英]Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold

I am trying to show snackbar on button click but due to some reasons facing an error message below.我试图在按钮单击时显示小吃栏,但由于某些原因,下面出现错误消息。

Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold.未处理的异常:使用不包含 Scaffold 的上下文调用 Scaffold.of()。

Am I missing anything?我错过了什么吗?

Code代码

class SignIn extends StatefulWidget {
  @override
  _SignInState createState() {
    return _SignInState();
  }
}

class _SignInState extends State<SignIn> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "Hello",
        home: Scaffold(
            body: Center(
                child: ListView(shrinkWrap: true, children: <Widget>[
              Center(
                  child: Form(
                key: _formKey,
                child: Column(children: <Widget>[                 
                  Container(
                    child: Column(
                      children: <Widget>[                        
                        Container(
                          child: Row(
                            children: <Widget>[
                              ElevatedButton(
                                  child: Text("Login"),
                                  onPressed: () {
                                    Scaffold.of(context).showSnackBar(
                                            SnackBar(
                                              content: Text("Hello there!"),
                                            ),
                                          );
                                  })
                            ],
                          ),
                        )
                      ],
                    ),
                  )
                ]),
              ))
            ]))));
  }
}

Use Scaffold key for showing snackbar.使用脚手架键显示小吃店。

class SignIn extends StatefulWidget {
  @override
  _SignInState createState() {
    return _SignInState();
  }
}

class _SignInState extends State<SignIn> {
  final _formKey = GlobalKey<FormState>();
  final _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Hello",
      home: Scaffold(
        key: _scaffoldKey,
        body: Center(
          child: ListView(
            shrinkWrap: true,
            children: <Widget>[
              Center(
                child: Form(
                  key: _formKey,
                  child: Column(children: <Widget>[
                    Container(
                      child: Column(
                        children: <Widget>[
                          Container(
                            child: Row(
                              children: <Widget>[
                                ElevatedButton(
                                    child: Text("Login"),
                                    onPressed: () async {
                                      _scaffoldKey.currentState.showSnackBar(
                                        SnackBar(
                                          content: Text("Hello there!"),
                                        ),
                                      );
                                    })
                              ],
                            ),
                          )
                        ],
                      ),
                    )
                  ]),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Try尝试

Material App (home: NewWidget()) Material App(主页:NewWidget())

In which the其中

NewWidget();新小部件();

is a stateless or stateful widget that returns Scaffold返回 Scaffold的无状态或有状态小部件

Create a new widget and paste all the code from Scaffold.创建一个新的小部件并粘贴 Scaffold 中的所有代码。 Then return the widget at home:然后在家里返回小部件:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不包含脚手架的上下文调用的 Scaffold.of() - Scaffold.of() called with a context that does not contain a Scaffold Flutter :此错误出现在使用不包含 Scaffold 的上下文调用的 Scaffold.of() 。 当我尝试展示小吃店时 - Flutter : this error appear Scaffold.of() called with a context that does not contain a Scaffold. when i try to show a snackbar 处理手势时引发了以下断言: Scaffold.of() 使用不包含 Scaffold 的上下文调用 - The following assertion was thrown while handling a gesture: Scaffold.of() called with a context that does not contain a Scaffold Snackbar 和异常:scaffold.of() - Snackbar and exception: scaffold.of() 上下文的颤振脚手架“不包含脚手架” - Flutter scaffold of context giving "does not contain a Scaffold" Flutter Scaffold.of(context).openDrawer() 不起作用 - Flutter Scaffold.of(context).openDrawer() doesn't work 错误:没有名为“nullOk”的命名参数 Scaffold.of(context, nullOk: true) - Error: No named parameter with the name 'nullOk' Scaffold.of(context, nullOk: true) Scaffold.of会找到遥远的窗口小部件吗? - Will Scaffold.of ever find distant widget anestors? 尝试使用 Scaffold.of(context) 使用 openDrawer() 找到 2 而不是 0 时出现太多位置 arguments 错误 - Too many positional arguments error when trying to use openDrawer() using Scaffold.of(context) finding 2 instead of 0 为什么传递BuildContext表示该Context中没有Scaffold? - Why does passing BuildContext say there is no Scaffold in that Context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM