Go_Router 将对象传递到新路由

Tot*_*Tot 23 android routes ios dart flutter

我想将对象从列表视图传递到子路线。似乎没有办法传递对象。

有什么办法吗?

GoRouter routes(AuthBloc bloc) {
 return GoRouter(
navigatorKey: _rootNavigatorKey,
routes: <RouteBase>[
  GoRoute(
    routes: <RouteBase>[
      GoRoute(
        path: loginURLPagePath,
        builder: (BuildContext context, GoRouterState state) {
          return const LoginPage();
        },
      ),
      GoRoute(
        path: homeURLPagePath,
        builder: (BuildContext context, GoRouterState state) =>
            const HomePage(),
        routes: <RouteBase>[
          GoRoute(
              path: feeURLPagePath,
              name: 'a',
              builder: (context, state) => FeePage(),
              routes: [
                /// Fee Details page
                GoRoute(
                  name: 'b',
                  path: feeDetailsURLPagePath,
                  builder: (BuildContext context, GoRouterState state) =>
                      const FeeDetails(),
                ),
              ]),
        ],
      ),
    ],
    path: welcomeURLPagePath,
    builder: (BuildContext context, GoRouterState state) =>
        const SplashPage(),
  ),
],
refreshListenable: GoRouterRefreshStream(bloc.stream),
debugLogDiagnostics: true,
initialLocation: welcomeURLPagePath,

          },
     );
     } 
Run Code Online (Sandbox Code Playgroud)

该错误表明未找到 FeeDetails 的初始匹配项!

kri*_*yaa 60

使用extra参数context.goNamed()

例子:

目的:

class Sample {
  String attributeA;
  String attributeB;
  bool boolValue;
  Sample(
      {required this.attributeA,
      required this.attributeB,
      required this.boolValue});}
Run Code Online (Sandbox Code Playgroud)

将 GoRoute 定义为

 GoRoute(
    path: '/sample',
    name: 'sample',
    builder: (context, state) {
      Sample sample = state.extra as Sample; // -> casting is important
      return GoToScreen(object: sample);
    },
  ),
Run Code Online (Sandbox Code Playgroud)

称其为:

Sample sample = Sample(attributeA: "True",attributeB: "False",boolValue: false)
context.goNamed("sample",extra:sample );
Run Code Online (Sandbox Code Playgroud)

接收它为:

class GoToScreen extends StatelessWidget {
  Sample? object;
  GoToScreen({super.key, this.object});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: Text(
        object.toString(),
        style: const TextStyle(fontSize: 24),
      )),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)