12

I want to pass object from the listview to the sub route. It seems no way to pass the object.

Is there any how to do it?

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,

          },
     );
     } 

The error says no initial match found for feeDetails!

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
EngineSense
  • 3,266
  • 8
  • 28
  • 44

1 Answers1

31

Use extra parameter in context.goNamed()

Example:

Object:

class Sample {
  String attributeA;
  String attributeB;
  bool boolValue;
  Sample(
      {required this.attributeA,
      required this.attributeB,
      required this.boolValue});}

Define GoRoute as

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

Call it as:

Sample sample = Sample(attributeA: "True",attributeB: "False",boolValue: false)
context.goNamed("sample",extra:sample );

Receive it as:

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),
      )),
    );
  }
}
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88