Home > Software engineering >  Flutter initial routes with parameters
Flutter initial routes with parameters

Time:04-29

I need to pass an argument for my initialRoute. I found this issue and tried it like this:

    initialRoute: AuthService.isLoggedIn() ? Views.home : Views.firstStart,
    onGenerateInitialRoutes: (String initialRouteName) {
      return [
        AppRouter.generateRoute(
          RouteSettings(
            name: AuthService.isLoggedIn() ? Views.home : Views.firstStart,
            arguments: notificationPayloadThatLaunchedApp,
          ),
        ),
      ];
    },
    onGenerateRoute: AppRouter.generateRoute,

This almost works. The problem I have is that somehow after calling this...

Navigator.pushReplacementNamed(
    context,
    Views.loading,
);

... my Multiprovider which is a parent of my GetMaterialApp is being called again which crashed my app because I call a function when initializing my providers:

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) {
            var dataProvider = DataProvider();
            dataProvider.init( // <- this is called again which should not happen
              context,
            );
            return dataProvider;
          },
        ),
      ],
      child: GetMaterialApp(
        title: 'Flutter Boilerplate',
        navigatorKey: Get.key,
        initialRoute: AuthService.isLoggedIn() ? Views.home : Views.firstStart,
        onGenerateInitialRoutes: (String initialRouteName) {
          return [
            AppRouter.generateRoute(
              RouteSettings(
                name: AuthService.isLoggedIn() ? Views.home : Views.firstStart,
                arguments: notificationPayloadThatLaunchedApp,
              ),
            ),
          ];
        },
        onGenerateRoute: AppRouter.generateRoute,
      ),
    );
  }

I feel like the way how I pass the initial argument is wrong. Is there any other/better way to get this done? Let me know if you need any more info!

CodePudding user response:

I think you misunderstand the usage of new API onGenerateInitialRoutes cause by the name it should load without the calling of

Navigator.pushReplacementNamed(
    context,
    Views.loading,
);

API at all. if you call this route from another widget, it means this is already the second route already. it should be the default route for your application. so it makes no sense to give a parameter to an initial route at all.

since you have used Provider Package, it is better to just get whatever argument(parameter) that you want to send via Provider API itself.

if you want to give hardcoded data, then just treat it as a normal Widget class.

home: MyHomePage(name:"parameter name",data:DataHome()), :

GetMaterialApp(
        title: 'Flutter Boilerplate',
        navigatorKey: Get.key,
        home: MyHomePage(name:"parameter name",data:DataHome()),
        onGenerateRoute: AppRouter.generateRoute,
      );

take note that if you calling Navigator.pushReplacementNamed API . your provider will be gone cause of the Flutter Widget Tree most Navigator API will create a new Widget tree level from the root. since the provider only provides for all its child only. so you cant use any provider data since you have a different ancestor.

so if you only have one page to go I suggest you use the home attribute in MaterialApp API cause it will treat your initial page as a child and Provider API can receive the data cause up in the Widget tree, it has an Ancestor Provider API:

MultiProvider(
      providers: [
        ChangeNotifierProvider(

if you want to move between pages via navigator after the main page. consider using NestedNavigator so flutter will not create a new route from the root of the widget tree. so you still can access Provider API's data.

guessing from your variable name. I assume that you want to handle a notification routing, check this deeplink

  • Related