问题描述
我将文本数据从第2页传递到第1页,如下所示:
void _awaitReturnValueFromSecondScreen(BuildContext context) async {
// start the SecondScreen and wait for it to finish with a result
final result = await Navigator.push(
context,MaterialPageRoute(
builder: (context) => MakeCircle(),));
// after the SecondScreen result comes back update the Text widget with it
setState(() {
text = result;
});
}
这是我的应用栏项目处理程序:
Future<void> handleClick(String value) async {
switch (value) {
case 'Circles':
_awaitReturnValueFromSecondScreen(context);
break;
当我尝试使用应用程序栏中的向左箭头退出时,错误弹出。 “失败的断言。数据!= null;必须为文本窗口小部件提供非null的String。” 当我使用浮动按钮退出时,它可以正常退出...我也希望也可以使用向左箭头退出...有什么想法吗?
解决方法
这可能不是解决问题的最优雅的方法,但目前仍然有效。
我向后退箭头分配了以下内容:
onPressed: () {
_sendDataBack(context);
Navigator.of(context).pop();
},
当我将文本字段的值设置为非null时,此方法有效。
,您必须使用on pop pop scope小部件并使用pop返回文本值。 用WillPopScope包装第二页小部件
WillPopScope(
onWillPop: () async {
Navigator.pop(context,text);
return false;
}child:Scaffold(),