簡體   English   中英

Go_Router 將對象傳遞給新路由

[英]Go_Router Pass Object to new route

我想將對象從列表視圖傳遞到子路由。 似乎沒有辦法傳遞對象。

有什么辦法嗎?

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 的初始匹配項!

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),
      )),
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM