1

I tried using named routes but it doesn't seem it work giving me an error "could not find a generator for route ("/homepage",null) for _MaterialAppState.I couldn't understand the working of namedRoutes in this case.

 import 'package:flutter/material.dart';
 import './home.dart';
 import './auth.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  Widget build(BuildContext context) {
   return MaterialApp(
     initialRoute: '/',
     routes: {
       '/': (BuildContext context) => Auth(),
       '/homepage': (BuildContext context) => Home(),
      },
    );
  }
}
//somewhere in auth.dart file
    RaisedButton(
                shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25.0)),
                        color: Theme.of(context).accentColor,
                child: Column(
                      children: <Widget>[
                        Icon(Icons.arrow_forward),
                      ],
                    ),
                    onPressed: () {
                      if (_email == "email" && _pass == '123') {
                        //Navigator.pushReplacementNamed(context,'/homepage');
                        Navigator.pushReplacement(context, MaterialPageRoute(
                          builder: (BuildContext context) => Home(),
                        ));
                      } else {
                        Scaffold.of(context).showSnackBar(SnackBar(
                          content:
                              Text("Please Enter Corrent Login Details"),
                          action: SnackBarAction(
                            label: "OK",
                            onPressed: () {
                              _controllerEmail.clear();
                              _controllerPass.clear();
                            },
                          ),
                        ));
                      }
                    },
                  ),

I need it to use the named route

Deepak Pawade
  • 150
  • 1
  • 13

1 Answers1

1

You should use:

Navigator.of(context).pushReplacementNamed('/homepage');

Look at the documentation for more details.

EDIT:
Use only 1 material app. Using two material apps (in main.dart and in Auth.dart) leads flutter to search the route in the closest MatertialApp (Auth.dart) and the route definition is in the highest instace (main.dart).
You should remove the MaterialApp from Auth.dart

AmazingBite
  • 302
  • 2
  • 16
  • thanks for the reply but ... 'Navigator.of(context).pushReplacementNamed('/homepage'); ... is not working. ... I/flutter (31934): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter (31934): The following assertion was thrown while handling a gesture: I/flutter (31934): Could not find a generator for route RouteSettings("/homepage", null) in the _WidgetsAppState. – Deepak Pawade Jul 22 '19 at 13:51
  • Try deleting the initialRoute parameter and use ```home: Auth(),``` instead. It caused me troubles in the past. – AmazingBite Jul 22 '19 at 14:00
  • still the same error... There must be either a small silly mistake or some bug – Deepak Pawade Jul 22 '19 at 14:12
  • 1
    Ok, I got it. The error appears because you have two instances of MaterialApp. You have defined the route only in the first one and Flutter search the route on the second one (because closer to the call). So try removing the second instance of MaterialApp and your problem should disapear. – AmazingBite Jul 22 '19 at 14:20