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
}