1

I am trying to make a reusable AlertDialog in Dart and was able to make one. The UI displays and it works well. The only problem in this code is that when I press the Save Button, it gives the following error: Unhandled Exception: type 'Future' is not a subtype of type 'Future'. Below is my AlertDialog class:

enum alertDialogAction { cancel, save }

class Dialogs {
  static Future<alertDialogAction> alertDialog(
    BuildContext context,
    String title,
    String body,
  ) {
    final action = showDialog(
        context: context,
        barrierDismissible: true,
        builder: (BuildContext context) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),
            ),
            title: Text(title),
            content: Text(body),
            actions: <Widget>[
              FlatButton(
                  onPressed: () =>
                      Navigator.pop(context,alertDialogAction.cancel),
                  child: Text("cancel")),
              RaisedButton(
                  color: Colors.blueAccent,
                  onPressed: () =>
                      Navigator.of(context).pop(alertDialogAction.save),
                  child: Text(
                    "save",
                    style: TextStyle(color: Colors.white),
                  )),
            ],
          );
        });
    return (action != null) ? action : alertDialogAction.cancel;
  }
 }

Here is how I call it in my index.dart:

 final action=await Dialogs.alertDialog(context,"title", "body");
 if (action == alertDialogAction.save){
      //code runs
      }
CoderUni
  • 5,474
  • 7
  • 26
  • 58
  • I think your textDialog method returns Future type. But in your Dialog class, you're having alertDialog. This will return Future type and now compare the equality with alertDialogAction.save. Can you please update your question with textDialog method ? – Vinoth Vino Mar 28 '20 at 03:38
  • @VinothVino Thank you for your reply, it seems that I copied the wrong code here. I copied my textDialog class's code. It is supposed to be await Dialogs.alertDialog. Sorry for the confusion. – CoderUni Mar 28 '20 at 03:41

1 Answers1

2

This is because you are returning a Future<dynamic> by declaring final action meanwhile you specified the return type of the static function alertDialog to be Future<alertDialogAction>.

To avoid such a problem in the future, always give a specific type to all the variable you declare.

Future<alertDialogAction> action = showDialog(
        context: context,
        barrierDismissible: true,
        builder: (BuildContext context) {
          return AlertDialog(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),
            ),
            title: Text(title),
            content: Text(body),
            actions: <Widget>[
              FlatButton(
                  onPressed: () =>
                      Navigator.pop(context,alertDialogAction.cancel),
                  child: Text("cancel")),
              RaisedButton(
                  color: Colors.blueAccent,
                  onPressed: () =>
                      Navigator.of(context).pop(alertDialogAction.save),
                  child: Text(
                    "save",
                    style: TextStyle(color: Colors.white),
                  )),
            ],
          );
        });

And call as such

alertDialogAction action=await Dialogs.alertDialog(context,"title", "body");
Loïc Fonkam
  • 2,284
  • 11
  • 25