使用带有 Flutter 后退按钮的 onGenerateRoute 时断言失败
Posted
技术标签:
【中文标题】使用带有 Flutter 后退按钮的 onGenerateRoute 时断言失败【英文标题】:Failed assertion when using onGenerateRoute with Flutter back button 【发布时间】:2019-08-14 00:52:34 【问题描述】:我正在使用 MaterialApp 中的 onGenerateRoute 在我的颤振应用中实现导航。
对于其中一条路线,当我按下 Flutter 在 Appbar 中提供的后退按钮时出现此错误。
E/flutter (22996): [错误:flutter/lib/ui/ui_dart_state.cc(148)] 未处理的异常:断言失败:布尔表达式不得 空
E/flutter (22996): #0 ModalRoute.willPop (包:flutter/src/widgets/routes.dart)E/flutter(22996):
E/flutter (22996): #1 NavigatorState.maybePop (包:flutter/src/widgets/navigator.dart:1964:57) E/flutter (22996):
E/flutter (22996): #2 Navigator.maybePop (包:flutter/src/widgets/navigator.dart:1291:34)
E/flutter (22996):#3 BackButton.build。 (package:flutter/src/material/back_button.dart:91:19)
你能帮我理解什么是错的吗?
我的路线设置 ->
Route<dynamic> routes(RouteSettings settings)
switch (settings.name)
case '/':
return MaterialPageRoute<dynamic>(
builder: (BuildContext context)
return HomePage();
,
);
break;
case '/contactlist':
return MaterialPageRoute<dynamic>(
builder: (BuildContext context)
return FriendsList();
,
);
break;
case '/ChatroomFormAdd':
return MaterialPageRoute<dynamic>(
builder: (BuildContext context)
return const ChatroomInfo(
mode: 'Add');
,
);
break;
case '/ChatroomFormEdit':
return MaterialPageRoute<dynamic>(
builder: (BuildContext context)
return const ChatroomInfo(
mode: 'Edit');
,
);
break;
case '/ChatroomFormView':
return MaterialPageRoute<dynamic>(
builder: (BuildContext context)
return const ChatroomInfo(
mode: 'View');
,
);
break;
case '/ChatroomFormApprove':
return MaterialPageRoute<dynamic>(
builder: (BuildContext context)
return const ChatroomInfo(
mode: 'Approve');
,
);
break;
case '/errorscreen':
return MaterialPageRoute<dynamic>(
builder: (BuildContext context)
return ErrorScreen();
,
);
break;
default:
return MaterialPageRoute<dynamic>(
builder: (BuildContext context)
return ChatDetail(chatroomID: chatroomID);
,
);
break;
当我从以下路线返回时,返回按钮适用于预期的所有路线:
Navigator.of(context).pushNamed('/ChatroomFormView');
【问题讨论】:
【参考方案1】:您使用了WillPopScope
小部件吗?如果是这样,您需要提供一个将Future<boolean>
返回到onWillPop
参数的函数。
WillPopScope(
onWillPop: ()
//need to return Future<bool> here
【讨论】:
以上是关于使用带有 Flutter 后退按钮的 onGenerateRoute 时断言失败的主要内容,如果未能解决你的问题,请参考以下文章
使用flutter webview作为主页并按下后退按钮关闭应用程序
如果包含 endDrawer,Flutter 2.0 appbar 后退按钮会消失