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

Issue

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;



Solution

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

Answered By – Nitrodon

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *