Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'Future<alertDialogAction>'
1,233
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");
Author by
CoderUni
Updated on December 19, 2022Comments
-
CoderUni over 1 year
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 myAlertDialog
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 }
-
Vinoth Vino about 4 yearsI think your textDialog method returns Future<dynamic> type. But in your Dialog class, you're having alertDialog. This will return Future<AlertDialogAction> type and now compare the equality with alertDialogAction.save. Can you please update your question with textDialog method ?
-
CoderUni about 4 years@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.
-