使用popUntil
方法时,当我试图将数据传递到上一个屏幕时,我收到以下错误。无法理解错误发生的原因:
[VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: Unsupported operation: Cannot modify unmodifiable map
以下是我要遵循的步骤:
1( 路由器:
Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case LoginViewRoute:
String navigatedScreen = settings.arguments as String;
return MaterialPageRoute(
settings: RouteSettings(name: LoginViewRoute, arguments: Map()),
builder: (context) => LoginPage(
navigatedPage: navigatedScreen,
));
}
}
2( 推屏:
Navigator.pushNamed(context, LoginViewRoute, arguments: HomeViewRoute).then((value) {
final arguments = ModalRoute.of(context)?.settings.arguments as Map;
bool result = arguments[IS_LOGIN_COMPLTED];
if (result) {
_bloc.onLoggedIn();
print(User().email);
}
});
3( 处理从推送屏幕返回的数据:
Navigator.of(context).popUntil((route) {
if (route.settings.name == widget.navigatedPage) {
(route.settings.arguments as Map)[IS_LOGIN_COMPLTED] = true;
return true;
} else {
return false;
}
在第3步,代码块之后,我面临错误:
[VERBOSE-2:ui_dart_state.cc(198(]未处理的异常:不支持操作:无法修改不可修改的地图
这是发生此错误的代码块(在步骤3中(:
(route.settings.arguments as Map)[IS_LOGIN_COMPLTED] = true;
已解决。确保您将弹出的屏幕的settings
不能具有const
值
在下面的代码中删除了settings
中的const
密钥,用户将被弹出。
case HomeViewRoute:
return MaterialPageRoute(
builder: (context) => HomePage(),
settings: RouteSettings(name: HomeViewRoute, arguments: {}),
);