Home > database >  Navigator.onGenerateRoute was null, but the route named "/login" was referenced
Navigator.onGenerateRoute was null, but the route named "/login" was referenced

Time:10-29

As I click on the "Continue" button it's not navigating me to the LogIn Screen.I tried multiple times but its not working and displaying me the above error sometimes.

What should be the correct code here for me to navigate to LogIn Screen. "Navigator.pushNamed(context,#What should I write here?)." (Also, I can provide the loginPage code as well if its required.)

**Body.dart**
```
import 'package:flutter/material.dart';
import 'package:flutter_catalog/constants.dart';
import 'package:flutter_catalog/default_button.dart';
import 'package:flutter_catalog/pages/login_page.dart';
import 'package:flutter_catalog/size_config.dart';
import '../components/splash_content.dart';


class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  int currentPage = 0;
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: SizedBox(
        width: double.infinity,
        child: Column(
          children: <Widget>[
            Expanded(
              flex: 5,
              child: PageView.builder(
                onPageChanged: (value) {
                  setState(() {
                    currentPage = value;
                  });
                },
                itemCount: splashData.length,
                itemBuilder: (context, index) => SplashContent(
                  image: splashData[index]["image"],
                  text: splashData[index]['text'],
                ),
              ),
            ),
            Expanded(
              flex: 2,
              child: Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: getProportionateScreenWidth(20)),
                child: Column(
                  children: <Widget>[
                    Spacer(),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: List.generate(
                        splashData.length,
                        (index) => buildDot(index: index),
                      ),
                    ),
                    Spacer(flex: 3),
                    DefaultButton(
                      text: "Continue",
                      press: () {
                        Navigator.pushNamed(context,**#What should I write here?**);
                      },
                    ),
                    Spacer(),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}



```
**Routes.dart**
```
class MyRoutes{
  static String loginRoute = "/login";
  static String homeRoute = "/home";
  static String HomeDetailRoute = "/detail";
  static String cartRoute = "/cart";
  static String welcomescreenRoute = "/welcome";
  static String SplashScreenRoute = "/splash";
}

**main.dart**

    void main() {
  setPathUrlStrategy();
  runApp(VxState(store: MyStore(), child: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var vxNavigator = VxNavigator(routes: {
      
      "/": (_, __) => MaterialPage(child: SplashScreen()),
      MyRoutes.homeRoute: (_, __) => MaterialPage(child: HomePage()),
      MyRoutes.SplashScreenRoute: (_, __) => MaterialPage(child: SplashScreen()),
      MyRoutes.HomeDetailRoute: (uri, _) {
        final catalog = (VxState.store as MyStore)
            .catalog
            .getById(int.parse(uri.queryParameters["id"]!));
        return MaterialPage(
            child: HomeDetailPage(
          catalog: catalog,
        ),
        );
      },
    
      MyRoutes.loginRoute: (_, __) => MaterialPage(child: LoginPage()),
      MyRoutes.cartRoute: (_, __) => MaterialPage(child: CartPage()),
    });
    (VxState.store as MyStore).navigator = vxNavigator;



CodePudding user response:

You appear to be using a VelocityX navigator instead of the standard Navigator included in Flutter. As such, navigation should be handled through VxNavigator.

As described in the VxNavigator documentation, you can push a route like this:

context.vxNav.push(Uri(path: MyRoutes.loginRoute));
// or VxNavigator.of(context), or (VxState.store as MyStore).navigator

CodePudding user response:

You need to include the routes in the MaterialApp (main.dart):

routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/': (context) => const FirstScreen(),
// When navigating to the "/second" route, build the SecondScreen widget.
'/second': (context) => const SecondScreen(),
 // *Your Body*
 '/body': (context) => Body(), // No const when statefulWidget
},

Then you can access it easy with Navigator.pushNamed(context, '/body');

  • Related