1

I am trying to add router with bottom navigation bar in CupertinoApp, but Navigator.pushNamed(context,anotherPage) is giving error

Could not find a generator for route RouteSettings("/anotherPage", null) in the _CupertinoTabViewState.

but Navigator.push(context, CupertinoPageRoute(builder: (context)=>AnotherPage())); is working Sample code:

return CupertinoApp(
  localizationsDelegates: <LocalizationsDelegate<dynamic>>[
    DefaultMaterialLocalizations.delegate,
    DefaultWidgetsLocalizations.delegate,
    DefaultCupertinoLocalizations.delegate,
  ],
  theme: CupertinoThemeData(brightness: Brightness.light),  
 onGenerateRoute: Router.generateRoute,
  initialRoute: splashScreen,
); }} 

//router class

    class Router {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case homeRoute:
        return CupertinoPageRoute(builder: (_) => CupertinoHomePage()); 
      case productDetails:
        final ProductDetails args = settings.arguments;
        return CupertinoPageRoute(
            builder: (_) =>
                ProductDetails(args.productsPojo, args.userId));
      case anotherPage:
        return MaterialPageRoute(builder: (_) => AnotherPage());   
        case splashScreen:
        return MaterialPageRoute(builder: (_) => SplashScreen());    
      default:
        return MaterialPageRoute(builder: (_) => UndefinedView(name: settings.name));
    }
  }
}
4
  • the bottom nav bar is from google codelabs Jun 24, 2020 at 12:18
  • This looks similar to stackoverflow.com/questions/51663793/… so have a look if you can find the answer there. Are the variables you use in the switch cases strings that match what you pass to pushNamed? Got no experience with CupertinoApp so it might be correct, but why are some routes CupertinoPageRoute and some MaterialPageRoute?
    – Sondre
    Jun 24, 2020 at 12:34
  • Sir, variables are never a problem. I have a constant class which stores all variables. In the past I was using metarialApp and that is working fine with routes. Now when I switch on, the CupertinoApp issue begins. And i also tried that solution but it also not working with Cupertino tab bar Jun 24, 2020 at 12:48
  • i think the issue is with BottomNavigationBar of CupertinoTabBar Jun 24, 2020 at 12:49

2 Answers 2

6

I struggled with this for some time. CupertinoTabView has a 'routes' property. Put your app routes here

return CupertinoTabView(
      routes: appRoutes,
      builder: (BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text(
              titles[currentRoute]
            ),
            trailing: FlatButton(
              child: Icon(Icons.search, color: Colors.white,),
              onPressed: openSearch,
            ),
          ),
          child: Material(
            child: Center(
              child: routes[currentRoute],
            ),
          ),
        );
      },
    );

appRoutes:

final appRoutes = {
  '/exampleRoute': (context) => ExampleRoute(),
  '/exampleRoute2': (context) => ExampleRoute2(),
}

You'll basically have to copy the routes you already declared in main.dart

3
  • 1
    yeah man it is working thanks for the help I will remember you lifetime Jul 29, 2020 at 5:22
  • Searched a longtime for that solution, thank you!
    – Fabrunet
    Dec 27, 2020 at 23:04
  • thanks its helped, i have an issue is i want to get the current page name of CupertinoTabView but i get null , any idea ? May 5, 2021 at 9:29
0

@applejeewce said right.

You can also set onGenerateRoutes in place of routes in this scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.