flutter Go_Router将对象传递到新路由

wko9yo5t  于 2022-12-19  发布在  Flutter
关注(0)|答案(1)|浏览(173)

我想把对象从列表视图传递到子路由。似乎没有办法传递对象。
有什么办法吗?

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,

          },
     );
     }

错误提示未找到feeDetails的初始匹配项!

cigdeys3

cigdeys31#

context.goNamed()中使用extra参数

示例:

    • 对象:**
class Sample {
  String attributeA;
  String attributeB;
  bool boolValue;
  Sample(
      {required this.attributeA,
      required this.attributeB,
      required this.boolValue});}
    • 将GoRoute定义为**
GoRoute(
    path: '/sample',
    name: 'sample',
    builder: (context, state) {
      Sample sample = state.extra as Sample; // -> casting is important
      return GoToScreen(object: sample);
    },
  ),
    • 称之为:**
Sample sample = Sample(attributeA: "True",attributeB: "False",boolValue: false)
context.goNamed("sample",extra:sample );
    • 接收方式:**
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),
      )),
    );
  }
}

相关问题