【发布时间】:2021-07-30 00:05:49
【问题描述】:
如官方文档中所述,我将值从一个页面传递到另一个页面。 https://flutter.dev/docs/cookbook/navigation/returning-data
我实现了两个不同的按钮。
-
提交按钮,返回一个字符串列表。
-
返回 null 的关闭按钮。
ElevatedButton( onPressed: () async { _formKey.currentState?.save(); if (_formKey.currentState!.validate()) { _formKey.currentState?.fields.forEach((key, value) { // return a list with the names widget._names.add(value.value); print(widget._names); }); Navigator.pop(context, widget._names); } }, child: Text('Speichern & Zurück')), OutlinedButton( onPressed: () { Navigator.pop(context, null); }, child: Text('Verwerfen & Zurück'))
该值是 Navigator.push() - 方法的返回值。
result = Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PlayerNames(
_currentBoySliderValue,
_currentGirlSliderValue)));
我通过简单的检查来捕获 null Future:
if (result != null) {
// does not catch Future<dynamic> with null content
}
不幸的是,Future(结果)只有在 Page PlayerNames() 从未被调用时才为 null,因为按下返回 null 的按钮不会使 Future 变为 null,而是使其内容变为 null。这会导致无意的行为,即如果用户在页面中未插入数据并使用按钮,这会导致返回 null,我的方法将在未来使用 null 值调用。
如果存储此信息的值是目标,是否有机会将 Future 设置为 null?我不想添加代码来检查未来的值是否为空,因为它并不总是允许有空数据。
【问题讨论】:
-
您应该在等待
PlayerNames的回复时添加await。如果您需要进一步说明,请告诉我。 -
那行得通,但我不知道为什么。你能澄清一下吗?
-
另外我必须将 Future
结果更改为 List ?结果;
标签: flutter dart null navigator