Search code examples
flutterdartproviderriverpodflutter-go-router

error: If the routerConfig is provided, all the other router delegates must not be provided (GoRouting Package)


I want to create an authentication flow with GoRoute and Riverpod.

the as the following:

  1. display a splash screen.
  2. while the splash screen is displayed check if the user has a token or not.
  3. based on the result either navigate the user to the login screen or the home screen.

when trying to run the application this error occurred:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building MaterialApp(dirty, state: _MaterialAppState#8a7c5):
If the routerConfig is provided, all the other router delegates must not be provided
'package:flutter/src/widgets/app.dart':
app.dart:1
Failed assertion: line 454 pos 14: '(routeInformationProvider ?? routeInformationParser ?? routerDelegate ?? backButtonDispatcher) == null'

AppRouting class:

class AppRouting {
  // final appService;
  final Ref ref;

  GoRouter get router => _routes;

  AppRouting({
    required this.ref,
  });

  late final appService = ref.watch(appServiceProvider);
  late final _routes = GoRouter(
    refreshListenable: appService,
    initialLocation: APP_PAGE.home.toPath,
    routes: [
      GoRoute(
        name: APP_PAGE.home.toName,
        path: APP_PAGE.home.toPath,
        // builder: (context, state) => const HomeScreen(),
        builder: (context, state) => const LoginScreen(),
      ),
      GoRoute(
        name: APP_PAGE.login.toName,
        path: APP_PAGE.login.toPath,
        builder: (context, state) => const LoginScreen(),
      ),
      GoRoute(
        name: APP_PAGE.resetPassword.toName,
        path: APP_PAGE.resetPassword.toPath,
        builder: (context, state) => const ResetPasswordScreen(),
      ),
      GoRoute(
        name: APP_PAGE.listView.toName,
        path: APP_PAGE.listView.toPath,
        builder: (context, state) => ListViewScreen(),
      ),
      GoRoute(
        name: APP_PAGE.palettQrCode.toName,
        path: APP_PAGE.palettQrCode.toPath,
        builder: (context, state) => PalettQrCode(),
      ),
      GoRoute(
        name: APP_PAGE.settings.toName,
        path: APP_PAGE.settings.toPath,
        builder: (context, state) => SettingScreen(),
      ),
      // GoRoute(
      //   name: AppRoutingConstans.girdViewRouteName,
      //   path: GridViewScreen.screenPath,
      //   builder: (context, state) => GridViewScreen(),
      // ),
      GoRoute(
        name: APP_PAGE.productPicker.toName,
        path: APP_PAGE.productPicker.toPath,
        builder: (context, state) => ProductPickUpScreen(),
      ),
      GoRoute(
        name: APP_PAGE.error.toName,
        path: APP_PAGE.error.toPath,
        builder: (context, state) => ErrorScreen(
          error: state.extra.toString(),
        ),
      ),
      GoRoute(
        name: APP_PAGE.splash.toName,
        path: APP_PAGE.splash.toPath,
        builder: (context, state) => const SplashScreen(),
      ),
    ],
    errorBuilder: (context, state) => ErrorScreen(
      error: state.error.toString(),
    ),
    redirect: (context, state) {
      final splashLocation = state.namedLocation(APP_PAGE.splash.toPath);
      final homeLocation = state.namedLocation(APP_PAGE.home.toPath);
      final loginLocation = state.namedLocation(APP_PAGE.login.toPath);

      final isLogedIn = appService.loginState;
      final isInitialized = appService.initialized;

      final isGoingToLogin = state.path == loginLocation;
      final isGoingToInit = state.path == splashLocation;

      print(state.path);

      // If not Initialized and not going to Initialized redirect to Splash
      if (!isInitialized && !isGoingToInit) {
        return splashLocation;
        // If not logedin and not going to login redirect to Login
      } else if (isInitialized && !isLogedIn && !isGoingToLogin) {
        return loginLocation;
        // If all the scenarios are cleared but still going to any of that screen redirect to Home
      } else if ((isLogedIn && isGoingToLogin) ||
          (isInitialized && isGoingToInit)) {
        return homeLocation;
      } else {
        // Else Don't do anything
        return null;
      }
    },
  );
}

// GoRouter configuration

final goRouterProvider = Provider<AppRouting>((ref) {
  return AppRouting(ref: ref);
});

in main.dart

class MyApp extends ConsumerStatefulWidget {
  const MyApp({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() => _MyAppState();
}

class _MyAppState extends ConsumerState<MyApp> {
  @override
  Widget build(BuildContext context) {
    final goRouter = ref.read(goRouterProvider).router;

    return MaterialApp.router(
      ...
      routerConfig: goRouter,
      routeInformationProvider: goRouter.routeInformationProvider,
      routeInformationParser: goRouter.routeInformationParser,
      routerDelegate: goRouter.routerDelegate,
      ...
    );
  }
}

Solution

  • From the error message:

    If the routerConfig is provided, all the other router delegates must not be provided.

    Change this:

    return MaterialApp.router(
      ...
      routerConfig: goRouter, // <== provided
      routeInformationProvider: goRouter.routeInformationProvider, // <== not needed
      routeInformationParser: goRouter.routeInformationParser, // <== not needed
      routerDelegate: goRouter.routerDelegate, // <== not needed
      ...
    );
    

    To this:

    return MaterialApp.router(
      ...
      routerConfig: goRouter,
      ...
    );