gpt4 book ai didi

flutter - 使用 ModalRoute 检索参数时出错

转载 作者:行者123 更新时间:2023-12-03 03:04:55 24 4
gpt4 key购买 nike

每次我调用 modalroute 以从命名路由中检索参数时,我都会得到 null。

这是代码片段:

Navigator.pushNamed(
context,
VerifyCode.routeName,
arguments: {'code': randValue,},
);

这是我检索参数的地方:
Widget build(BuildContext context) 
{
//Get the dialog provider to display information
alertViewModel = Provider.of<AlertViewModel>(context);

//Retrieve the route argument
final routeArgs =
ModalRoute.of(context).settings.arguments as Map<String, int>;

final securityCode = routeArgs['code']; <-- Error happens here

print('Code =' + securityCode.toString());
return Container()

}

我应该从 map 中获取一个整数,但我收到了错误:
 ════════ Exception Caught By widgets library ═══════════════════════════════════
The following NoSuchMethodError was thrown building VerifyCode(dirty, dependencies: [InheritedProvider<AlertViewModel>, _ModalScopeStatus], state: _VerifyCodeState#98992):
The method '[]' was called on null.
Receiver: null
Tried calling: []("code")
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
#1 _VerifyCodeState.build package:accidentapp/screens/verifycode.dart:42
#2 StatefulElement.build
package:flutter/…/widgets/framework.dart:4012
#3 ComponentElement.performRebuild package:flutter/…/widgets/framework.dart:3924
#4 Element.rebuild
package:flutter/…/widgets/framework.dart:3721 ═══════════════════════════════════════════════════════════════════

routeArgs 始终为空。我尝试使用类来传递值而不是 map ,但我遇到了同样的错误。不知道我做错了什么。

最佳答案

将设置传递给 MaterialPageRoute

Route<dynamic> generateRoute(RouteSettings settings) {
return MaterialPageRoute(
builder: (BuildContext context) {
//some custom code
return Page();
},
settings: settings
);
}

关于flutter - 使用 ModalRoute 检索参数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495187/

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