编辑:重大变化Go_路由器7.0.0 https://docs.google.com/document/d/10Xbpifbs4E-zh6YE5akIO8raJq_m3FIXs6nUGdOspOg/edit
简而言之
Below Go Router 7 i.e < 7.0.0 use `params`, `queryParams`
Above Go Router 7 i.e >=7.0.0 use `pathParameters`, `queryParameters`
Summary:
有以下三种方式:pathParameters
, queryParameters
, extra
- Using
pathParameters
- 当你事先知道参数的数量时
- Usage :
path = '/routeName/:id1/:id2'
- Using
queryParameters
- 当你不确定参数的数量时
- Usage :
path = '/routeName'
- Using
extra
解释:
1. 使用pathParameters
当您事先知道参数数量时使用pathParameters
支撑context.goNamed()
将其定义为:
GoRoute(
path: '/sample/:id1/:id2', // ???? Defination of params in the path is important
name: 'sample',
builder: (context, state) => SampleWidget(
id1: state.pathParameters['id1'],
id2: state.pathParameters['id2'],
),
),
称其为:
ElevatedButton(
onPressed: () {
var param1 = "param1";
var param2 = "param2";
context.goNamed("sample", pathParameters: {'id1': param1, 'id2': param2});
},
child: const Text("Hello"),
),
接收它为:
class SampleWidget extends StatelessWidget {
String? id1;
String? id2;
SampleWidget({super.key, this.id1, this.id2});
@override
Widget build(BuildContext context) {
...
}
}
2. 使用queryParameters
您可以访问queryParameters
in the context.goNamed()
功能。最好的事情是queryParameters
是您不必在路线路径中显式定义它们,并且可以使用state.queryParameters
方法。您可以添加各种与用户相关的数据作为查询参数。
将其定义为:
GoRoute(
name: "sample",
path: "/sample",
builder: (context, state) => SampleWidget(
id1: state.queryParameters['id1'],
id2: state.queryParameters['id2'],
),
)
称其为:
ElevatedButton(
onPressed: () {
var param1 = "param1";
var param2 = "param2";
context.goNamed("sample", queryParameters: {'id1': param1, 'id2': param2});
},
child: const Text("Hello"),
),
接收它为:
class SampleWidget extends StatelessWidget {
String? id1;
String? id2;
SampleWidget({super.key, this.id1, this.id2});
@override
Widget build(BuildContext context) {
...
}
}
3. 使用extra
当你想通过一个model/object
路线之间
GoRoute(
path: '/sample',
builder: (context, state) {
Sample sample = state.extra as Sample; // ???? casting is important
return GoToScreen(object: sample);
},
),
Refer https://stackoverflow.com/a/74813017/13431819 https://stackoverflow.com/a/74813017/13431819为了通过object
路线之间