我需要通过Navigator.pushNamed
方法传递两个参数。我尝试了以下方法,但没有成功。
Navigator.of(context).pushNamed('/loading', arguments:id, apikey);
你可以像这样传递一个数组。欲了解更多详情,请查看此回答
Navigator.of(context).pushNamed('/loading', arguments:[id, apikey]);
可以使用.pushNamed()函数中的映射传递多个参数,如下所示:
Navigator.of(context).pushNamed('/loading', arguments: {'id': id, 'apikey': apikey});
或
Navigator.pushNamed(context, '/loading', arguments: {'id': id, 'apikey': apikey});
或
你可以像这样传递数组:
Navigator.of(context).pushNamed('/loading', arguments: [id, apikey]);
将MaterialApp
中的onGenerateRoute
修改为如下所示:
MaterialApp(
onGenerateRoute: (settings) {
if (settings.name == '/page2') {
final data = settings.arguments as Map<String, dynamic>;
return MaterialPageRoute(
builder: (_) => Page2(
id: data['id'],
apiKey: data['apiKey'],
),
);
}
return null;
},
)
这就是你应该如何传递一个Map
。
Navigator.pushNamed(
context,
'/page2',
arguments: {'id': your_id, 'apiKey': your_api_key},
);
这是你的Page2
小部件:
class Page2 extends StatelessWidget {
final String id;
final String apiKey;
const Page2({Key key, this.id, this.apiKey}) : super(key: key);
@override
Widget build(BuildContext context) => Container();
}