AlertDialog error after upgrading flutter

Viewed 571

I used AlertDialog to notify user about the internet availability. It was working fine until I upgrade my flutter using command flutter upgrade. Snapshot of the program is as follows:

connectivitySubscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult connresult) {
      if (connresult == ConnectivityResult.none) {
        dialogshown = true;
        showDialog(
          context: context,
          barrierDismissible: false,
          builder: AlertDialog(
            title: Text(
              "Error",
            ),
            content: Text(
              "No Data Connection Available.",
            ),
            actions: <Widget>[
              FlatButton(
                onPressed: () => {
                  SystemChannels.platform.invokeMethod('SystemNavigator.pop'),
                },
                child: Text("Exit."),
              ),
            ],
          ),
        );
      } else if (_previousResult == ConnectivityResult.none) {
        checkinternet().then((result) {
          if (result == true) {
            if (dialogshown == true) {
              dialogshown = false;
              Navigator.pop(context);
            }
          }
        });
      }

      _previousResult = connresult;
    });

The error is in the

builder: AlertDialog

of showDialog function. Before upgrading flutter it was child: AlertDialog and was working fine, and after upgrading it is showing error. The quick fix suggestion by flutter makes it "builder: AlertDialog" still it is showing error. The error is "The argument type 'AlertDialog' can't be assigned to the parameter type 'Widget Function(BuildContext)'.". There are also Strike through line on every FlatButton and RaisedButton in my complete project with warning message "'FlatButton' is deprecated and shouldn't be used. Use TextButton instead".

Please help me solve this problem. If I need to change to my previous version of flutter, let me know how to do? Thanks

3 Answers

Change

builder: AlertDialog

to

builder: (_) => AlertDialog()
  1. Use builder: (_) => AlertDialog()

  2. Change any occurrences of FlatButton and RaisedButton in your code to TextButton and set properties according to your needs.

You could downgrade Flutter (not really recommended in your case) to the last active version for the current channel by opening a Terminal and type

flutter downgrade

Actually ,Here real problem is same context reference which is used for pop the dialog

Basically we should separate allice name need to create for showDialog that is below example name

dialogContex

Solution:

  showDialog(
    context: context,
    builder: (dialogContex) {});

Solution With Example :

 showDialog(
    context: context,
    builder: (dialogContex) {
      return Dialog(
        child: Container(
          margin: const EdgeInsets.all(8.0),
          child: Form(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                const Text('Please Confirm'),
                const SizedBox(
                  height: 25.0,
                ),
                const Text('Are you sure to remove?'),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    TextButton(
                        onPressed: () {
                          Navigator.of(dialogContex).pop();
                        },
                        child: const Text('Yes')),
                    TextButton(
                        onPressed: () {
                          Navigator.of(dialogContex).pop();
                        },
                        child: const Text('No'))
                  ],
                )
              ],
            ),
          ),
        ),
      );
    });
Related