当我点击按钮时 该值是 Navigator.push() 方法的返回值。 “因为按下返回 null 的 Button,不会使 Future 变为 null,而是使其内容变为 null。这会导致无意的行为”
enter image description here 主屏幕
PopupMenuButton(
icon: Icon(Icons.more_vert,color: Colors.black,),
offset: Offset(0, 40),
itemBuilder: (context) => [
PopupMenuItem(
child: Text('View Cart'),
onTap: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CartScreen(cart: cart)),
) ;
if(result)
{
setState(() {
});
}
},
),
购物车屏幕
WillPopScope(
onWillPop: () {
Navigator.pop(context,true);
return new Future(() => false);;
},
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
iconTheme: IconThemeData(color: Colors.black),
elevation: 0,
),
当我单击按钮导航页面时
最佳答案
我有这样的例子:
主页
await Navigator.of(context).pushNamed(
PageNames.routeTest,
).then(
(result) {
if (result != null && result as bool) {
[...do something...]
}
}
},
);
测试页
可能只是一个
/// Could be just like :
Navigator.of(context).pop(true);
/// Or
onWillPop: () {
Navigator.of(context).pop(true);
return true;
},
尝试在 future 函数中使用 .then
并使用 Navigator.of(context).pop
。告诉我这是否有效
关于flutter - 推送返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77314859/