首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Flutter未处理的异常: NoSuchMethodError:在null上调用了getter 'currentState‘

Flutter未处理的异常: NoSuchMethodError:在null上调用了getter 'currentState‘
EN

Stack Overflow用户
提问于 2020-06-09 11:34:23
回答 1查看 427关注 0票数 0

当我在发送数据时使用showSnackBar时,我遇到了这个问题(颤动未处理的异常: NoSuchMethodError:在null上调用了getter 'currentState‘)。它出现在数据发送之后,我不知道我到底在哪里做错了

代码语言:javascript
运行
复制
class ProductNotifyDialog {
  BuildContext context;
  Product product;
  GlobalKey<ScaffoldState> scaffoldKey;

  ProductNotifyDialog({
    this.context,
    this.product,
  }) {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(15.0))),
//            contentPadding: EdgeInsets.symmetric(horizontal: 20),
            titlePadding: EdgeInsets.fromLTRB(0, 15, 16, 15),
            title: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.all(25.0),
                  child: Container(
                    width: MediaQuery.of(context).size.width,
                    child: Text(
                      S
                          .of(context)
                          .do_you_want_us_to_notify_when_it_is_in_stock_again,
                      style: Theme.of(context).textTheme.headline4,
                      textAlign: TextAlign.center,
                    ),
                  ),
                ),
              ],
            ),
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    width: 120,
                    height: 45,
                    child: RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      color: Colors.grey,
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      child: Text(
                        S.of(context).no,
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                  SizedBox(width: 25),
                  Container(
                    width: 120,
                    height: 45,
                    child: RaisedButton(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10.0),
                      ),
                      color: Theme.of(context).accentColor,
                      onPressed: () {
                        _submit();
                        //_con.notifyAboutProduct(widget.product);
                        Navigator.pop(context);
                      },
                      child: Text(
                        S.of(context).yes,
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                ],
                mainAxisAlignment: MainAxisAlignment.center,
              ),
              SizedBox(height: 10),
            ],
          );
        });
  }

  void _submit() {
    repo.notifyUnavailableProduct(product).then((value) {
      scaffoldKey.currentState.showSnackBar(SnackBar(
        content: Text(S.current.your_request_has_been_submitted),
      ));
    });
    Navigator.pop(context);
  }}

我试图在发送数据后在snackbar中显示消息,但遇到此错误,有人能帮助我吗?

EN

回答 1

Stack Overflow用户

发布于 2020-06-09 12:53:50

scaffoldKey必须包含该值,并且必须在Scaffold key属性中输入scaffoldKey。

代码语言:javascript
运行
复制
GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62274577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档