flutterdartroutesflutter-go-routergorouter

Equivalent of Navigator Arguments in Flutter Go Router


What will be equivalent of the below Flutter Navigator in terms of Go Router?

    Navigator.pushNamed(
       context,
       Routes.CHANNEL_PAGE,
       arguments:ChannelPageArgs(
         channel:channel,
          initialMessage:message,
        ),
    );

Usually Go Router is based on parameters from the path. But the above example is based on a object instead of primitive parameters.

        GoRoute(
          path: '/profile/:id',
          builder: (context, state) => ProfilePage(id: state.params['id']!),
        ),

Solution

  • you can pass object as an extra like this:

    final args = ChannelPageArgs(
         channel:channel,
          initialMessage:message,
        )
    context.pushNamed('profile', extra: args)

    in your goRouter:

          GoRoute(
                  path: 'profile',
                  name: 'profile',
                  pageBuilder: (context, state) {
                    final args = state.extra as ChannelPageArgs;
                    return MaterialPage(
                      key: state.pageKey,
                      child: ProfilePage(
                        args: args,
                      ),