gpt4 book ai didi

dart - 将 onGenerateRoute 与 Flutter 后退按钮一起使用时断言失败

转载 作者:IT王子 更新时间:2023-10-29 06:34:15 26 4
gpt4 key购买 nike

我正在使用 MaterialApp 中的 onGenerateRoute 在我的 flutter 应用程序中实现导航。

对于其中一条路线,当我按下 Appbar 中 Flutter 提供的后退按钮时出现此错误。

E/flutter (22996): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Failed assertion: boolean expression must not be null

E/flutter (22996): #0 ModalRoute.willPop (package:flutter/src/widgets/routes.dart) E/flutter (22996):

E/flutter (22996): #1 NavigatorState.maybePop (package:flutter/src/widgets/navigator.dart:1964:57) E/flutter (22996):

E/flutter (22996): #2 Navigator.maybePop (package: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');

最佳答案

你用过WillPopScope吗?小部件?如果是这样,您需要提供一个返回 Future<boolean> 的函数至 onWillPop参数。

WillPopScope(
onWillPop: () {
//need to return Future<bool> here
}
}

关于dart - 将 onGenerateRoute 与 Flutter 后退按钮一起使用时断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55309715/

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