當我嘗試洗掉該專案時遇到問題,但它不接受洗掉該專案。我正在使用 ConfirmDismiss。

錯誤:
未處理的例外:“Null”型別不是“bool”型別的子型別 _DisMissState.build.. (package:advanced_pa??rt_2/dismissible.dart:84:30)
_DismissibleState._handleDismissStatusChanged (package:flutter/src/widgets/dismissible.dart:498:11)
代碼:
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;
}
},
uj5u.com熱心網友回復:
這是因為當您呼叫以下代碼時:
final bool res = await showDialog(...);
它將回傳一個空值,參見https://api.flutter.dev/flutter/material/showDialog.html
而且,您在單擊按鈕時也不提供任何回傳值:
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
'cancel',
)),
你不能使用Navigator.of(context).pop();. 相反,您需要使用:
Navigator.pop(context, false);
或者
Navigator.pop(context, true);
然后,在此之后,您需要通過在 null 時提供默認值來處理來自 show dialog 的可為 null 的回傳值。像這樣的東西:
bool res = await showDialog(...);
if(res == null) res = false;
uj5u.com熱心網友回復:
showDialog 可以null在對話框關閉時回傳(例如用戶在對話框外單擊),因此您必須通過將型別更改為:
final bool? res = await showDialog(/* your code */);
然后在下面的邏輯中,您必須檢查 null:
if(res == null) {
// handle dismiss
} else if (res == false) {
// handle cancel
} else {
// handle confirm/true
}
正如評論中提到的@Er1,您還需要傳遞true或false彈出對話框時:
Navigator.of(context).pop(true);
// or
Navigator.of(context).pop(false);
以上仍然適用,因為barrierDismissible是true默認。
uj5u.com熱心網友回復:
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),
))
],
);
},
);
添加false和true到pop()使showDialog回傳一個布林值。
但請記住,如果您沒有barrierDismissible進入showDialog,如果您在對話框外點擊,則false可能會null回傳。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382764.html
