go_router 页面跳转 参数传递

918 阅读1分钟
  routes: <GoRoute>[
          GoRoute(
              name: 'family',
              path: 'family/:fid',
              builder: (BuildContext context, GoRouterState state) {
                return FamilyScreen(
                  fid: state.pathParameters['fid']!,
                  asc: state.uri.queryParameters['sort'] == 'asc',
                );
              }
          ),
        ],

1 通过state.pathParameters 获取path上设置的参数,比如fid,也可以设置多个参数 比如path 设置为 family/:fid/:aid 就可以通过 state.pathParameters['fid'] 和 state.pathParameters['aid'] 分别获取path上设置的fid 跟 aid

2 跳转方式

context.go('/family/${entry.key}

或者

context.goNamed('family',
                pathParameters: <String, String>{'fid': fid},
                queryParameters: newQueries)