how to Implement Authentication and Navigation in Flutter using go_router and redux

31 Views Asked by At

I'm using redux to manage the state, so how to get state in go_router, example code:

class MainApp extends StatelessWidget {
  const MainApp({super.key});
  @override
  Widget build(BuildContext context) {
    final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
    return StoreProvider<AppState>(
      store: store,
      child: MaterialApp(
        title: 'Provider Demo',
        navigatorKey: navigatorKey,
        onGenerateRoute: (RouteSettings settings) {
          final String username =
              StoreProvider.of<AppState>(context).state.username;

this dose not work, Error: No StoreProvider found. To fix, please try: * Wrapping your MaterialApp with the StoreProvider, rather than an individual Route * Providing full type information to your Store, StoreProvider and StoreConnector<State, ViewModel>

          final Map<String, WidgetBuilder> routes = {
            '/': (context) => const HomePage(),
            '/login': (context) => const Login(),
            '/404': (context) => const NotFound(),
          };
          final WidgetBuilder builder =
              routes[settings.name!] ?? (context) => const NotFound();
          return MaterialPageRoute(builder: builder, settings: settings);
        },
      ),
    );
  }
}

how to get state in onGenerateRoute? or is there any other way to implement.

0

There are 0 best solutions below