gpt4 book ai didi

flutter - 导航器使用 pushNamed 传递参数

转载 作者:IT老高 更新时间:2023-10-28 12:36:48 27 4
gpt4 key购买 nike

之前可能有人问过,但我找不到,但是如何将参数传递给命名路由?

这就是我构建路线的方式

Widget build(BuildContext context) {
return new Navigator(
initialRoute: 'home/chooseroom',
onGenerateRoute: (RouteSettings settings) {
WidgetBuilder builder;
switch (settings.name) {
case 'home/chooseroom':
// navigates to 'signup/choose_credentials'.
builder = (BuildContext _) => new ChoosePage();
break;
case 'home/createpage':
builder = (BuildContext _) => new CreateRoomPage();
break;
case 'home/presentation':
builder = (BuildContext _) => new Presentation();
break;
default:
throw new Exception('Invalid route: ${settings.name}');
}
return new MaterialPageRoute(builder: builder, settings: settings);
},
);

这就是你的称呼Navigator.of(context).pushNamed('home/presentation')

但是如果我的小部件是 new Presentation(arg1, arg2, arg3)

最佳答案

不需要onGenerateRoute。只需使用

var exampleArgument = 'example string';

Navigator.pushNamed(
context,
'/otherscreen',
arguments: {'exampleArgument': exampleArgument},
);

并提取参数如下:

@override
Widget build(BuildContext context) {
final arguments = (ModalRoute.of(context)?.settings.arguments ?? <String, dynamic>{}) as Map;

print(arguments['exampleArgument']);

return Scaffold(...);
}

关于flutter - 导航器使用 pushNamed 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53304340/

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