Search code examples
flutterdartdialogflutter-alertdialog

Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<alertDialogAction>'


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
      }

Solution

  • 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");