gpt4 book ai didi

dart - 如何从 dart 中的函数返回 Future 或任何其他类型

转载 作者:IT老高 更新时间:2023-10-28 12:42:57 30 4
gpt4 key购买 nike

我需要处理我的一个表单条目小部件上的后退按钮按下。这就是我实现 WillPopScope 的 onWillPop 方法的方式:

Future<bool> _onWillPop() {
if (changed) {
return showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Save'),
content: new Text("Do you want to save the changes?"),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(true),
child: new Text('No'),
),
new FlatButton(
onPressed: () {
Navigator.of(context).pop(false);
saveMeeting();
},
child: new Text('Yes'),
),
],
),
) ??
false;
} else {
print("No changes");
Navigator.of(context).pop(true);
//return some future null from here ????
}
}

这部分代码正在运行,但出现异常:

[ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 7374): Failed assertion: boolean expression must not be null

我怎样才能正确地实现它?

最佳答案

我遇到了完全相同的问题。

我通过返回 Future.value(false); 解决了它如果值为真,我会得到黑屏。

Future<bool> _onBackPressed() {
if (!widget.editing) {
Navigator.pop(context, true);
return Future.value(false);
} else {
return showDialog(...

关于dart - 如何从 dart 中的函数返回 Future 或任何其他类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51478405/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com