BigPP
BigPP

Reputation: 618

Flutter Navigator and Routing in separate files showing error: Could not find a generator for route RouteSettings("/details", null) in the _Widget

I am working on this Flutter app and trying to make routing works on separate files. Now I have a login page, and in it, there is a login button. When this login button is clicked, the app should navigate to the HomePage. I looked at a few tutorials, but they all put navigators and other widgets on the same page.

In my case, my file structure is:

project file structure in lib

and in routes.dart:

import 'package:flutter/material.dart';
import 'widgets/login.dart';
import 'widgets/homepage.dart';

void main() {
  runApp(Nav2App());
}

class Nav2App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        '/': (context) => Login(),
        '/homepage': (context) => HomePage(),
      },
    );
  }
}

In login.dart:

child: TextButton(
          style: ButtonStyle(
              ...
              ))),
          child: const Text('Login'),
          onPressed: () {
            Navigator.pushNamed(
              context,
              '/homepage',
            );
          },
        ),
      ),

I think I need to do something in main.dart, like:

  Widget build(BuildContext context) {
    return MaterialApp(
      // debugShowCheckedModeBanner: false,
      title: 'Flutter Login Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyLoginPage(title: 'login page'),
      routes: ***something***
    );
  }

Upvotes: 0

Views: 1257

Answers (3)

Gbenga B Ayannuga
Gbenga B Ayannuga

Reputation: 2792

   class Nav2App extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
         initialRoute: '/',
          routes: {
            '/': (context) => Login(),
            '/homepage': (context) => HomePage(),
          },
        );
      }
    }

Upvotes: 1

Kent
Kent

Reputation: 2557

You will want to replace the login page with the homepage. So use push replacement. That way they won’t get a back button in the top left.

As you said you updated the /details to /homepage in the comments above. But still not working make sure you do a full reload not just a hot reload.

Navigator.pushReplacementNamed(context, "/homepage");

Do not make a second material app in your your code like shows above. When you create another material app you also get a whole new navigator stack.

Use Scaffold instead. 1 material app is all you need.

Upvotes: 2

Gourango Sutradhar
Gourango Sutradhar

Reputation: 1619

There is no navigator for your /details name.

Change your login code like below:

child: TextButton(
          style: ButtonStyle(
              ...
              ))),
          child: const Text('Login'),
          onPressed: () {
            Navigator.pushNamed(
              context,
              '/homepage',
            );
          },
        ),
      ),

Upvotes: 0

Related Questions