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

时间:2020-06-09 03:34:23

标签: flutter snackbar

当我在发送数据时使用showSnackBar时,我遇到了这个问题(Flutter未处理的异常:NoSuchMethodError:在null上调用了吸气剂“ currentState”。)数据发送后就出现了,不知道我到底在哪里做错

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);
  }}

发送数据后,我试图在小吃栏中显示消息,但出现此错误,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

Scaffold key property