I have a problem when I try to delete the item and it doesn't accept to delete the item. I am using ConfirmDismiss.
Error:
Unhandled Exception: type 'Null' is not a subtype of type 'bool' _DisMissState.build.. (package:advanced_part_2/dismissible.dart:84:30)
_DismissibleState._handleDismissStatusChanged (package:flutter/src/widgets/dismissible.dart:498:11)
Code:
confirmDismiss: (DismissDirection dir) async {
if (dir == DismissDirection.startToEnd) {
// AlertDialog ad =
final bool res = await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Are You Sure you want to delete'),
actions:<Widget> [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
'cancel',
)),
ElevatedButton(
onPressed: () {
setState(() {
genList.removeAt(index);
});
Navigator.of(context).pop();
},
child: Text(
'Delete',
style: TextStyle(color: Colors.red),
))
],
);
},
);
return res;
} else {
return true;
}
},
CodePudding user response:
It is because when your calling the following code:
final bool res = await showDialog(...);
it will return a null value, see https://api.flutter.dev/flutter/material/showDialog.html
And, you also not giving any return value when clicking the button:
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
'cancel',
)),
You can't use Navigator.of(context).pop();
. Instead, you need to use either:
Navigator.pop(context, false);
or
Navigator.pop(context, true);
Then, after that, you need to handle the nullable returned value from show dialog by giving a default value when null. Something like this:
bool res = await showDialog(...);
if(res == null) res = false;
CodePudding user response:
showDialog can return null
when the dialog is dismissed (e.g. user clicks outside the dialog) so you've to account for that by changing the type to:
final bool? res = await showDialog(/* your code */);
then in your logic below, you have to check for null:
if(res == null) {
// handle dismiss
} else if (res == false) {
// handle cancel
} else {
// handle confirm/true
}
As @Er1 mentioned in the comment, you'll also need to pass true
or false
when popping the dialog:
Navigator.of(context).pop(true);
// or
Navigator.of(context).pop(false);
The above still applies since barrierDismissible
is true
by default.
CodePudding user response:
final bool res = await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Are You Sure you want to delete'),
actions:<Widget> [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(false);
},
child: Text(
'cancel',
)),
ElevatedButton(
onPressed: () {
setState(() {
genList.removeAt(index);
});
Navigator.of(context).pop(true);
},
child: Text(
'Delete',
style: TextStyle(color: Colors.red),
))
],
);
},
);
Add false
and true
to the pop()
to make the showDialog
return a boolean.
But keep in mind if you don't set barrierDismissible
in showDialog
to false
you can get null
returned if you tap outside of the dialog.