【问题标题】:Unhandled Exception: Scaffold.of() called with a context that does not contain a Scaffold未处理的异常:使用不包含 Scaffold 的上下文调用 Scaffold.of()
【发布时间】:2021-04-15 10:35:22
【问题描述】:

我试图在单击按钮时显示小吃栏,但由于某些原因,下面出现错误消息。

未处理的异常:Scaffold.of() 调用的上下文不 包含一个脚手架。

我错过了什么吗?

代码

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

【问题讨论】:

  • onPressed回调函数需要异步吗?将从不同的线程调用异步调用,届时它会获得正确的 Scaffold 上下文吗?您能否尝试使该回调仅正常同步。
  • 对不起,那是一种类型。 onPress 应该没有异步。
  • 这不起作用,因为showSnackBar 已被贬值

标签: flutter dart


【解决方案1】:

使用 Scaffold 键显示小吃吧。

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

【讨论】:

    【解决方案2】:

    试试

    Material App(主页:NewWidget())

    其中

    NewWidget();

    返回 Scaffold

    的无状态或有状态小部件

    创建一个新的小部件并粘贴 Scaffold 中的所有代码。然后将小部件归还给家里:

    【讨论】:

    • 你能举个例子吗?
    猜你喜欢
    • 2018-12-20
    • 2020-12-01
    • 2021-11-05
    • 2021-02-14
    • 2015-07-29
    • 1970-01-01
    • 2021-01-28
    • 2017-10-17
    • 1970-01-01
    相关资源
    最近更新 更多