0

I have this problem (Flutter Unhandled Exception: NoSuchMethodError: The getter 'currentState' was called on null.)when i am using showSnackBar while sending data. It appear right after the data get send, dont understand where exactly i am doing wrong

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

I am trying to show message in snackbar after sending data but getting this error, Can anyone help ?

Rock
  • 511
  • 2
  • 7
  • 18

1 Answers1

0

The scaffoldKey must contain the value and the scaffoldKey must be entered in the Scaffold key property.

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

Scaffold key property

Miko
  • 643
  • 8
  • 13