Could not find a generator for route
FlutterFlutter Problem Overview
I´m newbie to flutter and reveice one exception about route and paginator in Flutter.
EXCEPTION CAUGHT BY GESTURE
The following assertion was thrown while handling a gesture:
Could not find a generator for route "/listadecompras" in the _MaterialAppState.
Follow a excerpt from code:
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
// ...
return new ListTile(
onTap: () {
Navigator.pushNamed(context, "/listadecompras");
},
// ...
}
class ListaDeCompras extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
// ...
}
}
void main() {
runApp(new MaterialApp(
home: new MyApp(),
routes: <String, WidgetBuilder>{
"/listadecompras": (BuildContext context) => new ListaDeCompras()
}
));
}
Please, someone could send some advice? thanks in advance for your attention
Flutter Solutions
Solution 1 - Flutter
Because Of instantiated two MaterialApp
widgets. You need to remove the one in MyApp
class and may change it to Scaffold
Example:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
// ...
return new ListTile(
onTap: () {
Navigator.pushNamed(context, "/listadecompras");
},
// ...
}
THE PROBLEM IS YOUR CODE IS - the route is trying to resolve for the nearest MaterialApp
which has no route definition. That said you should use only one MaterialApp
as the root of your widget tree.
Solution 2 - Flutter
try this :
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new ListaDeCompras(),
),
);
},
Solution 3 - Flutter
If a new file is created and you are defining a route for that new file, its better to re-run the app rather than hot reloading.It worked for me!
Solution 4 - Flutter
flutter clean
and run again, is working for me.
Solution 5 - Flutter
I had a similar issue because I defined the route as '/listadecompras' and then called Navigator.pushNamed(context, "listadecompras");
without the forward slash.
It isn't probably the same scenario than this question but it shows the same error.
Solution 6 - Flutter
It happened to me, but in my case there was a single MaterialApp
widget instantiated, so I wasn't able to find the cause.
I noticed that in my App, all calls to Navigator where done the same, with:
Navigator.of(context).pushNamed('/route-name');
All, except the first one, which was like this:
Navigator.pushNamed(context, '/route-name');
After changing the above line to be equal to the others (using .of(context)
), the problem got solved!
Note
Until now I had no problems with navigation, it happened once tried to navigate to a new view from a dialog.
If you have only one MaterialApp, and you cannot fix this issue, try to check your navigation chain, and use the same way of calling Navigator in every place.
Bye
Solution 7 - Flutter
@aziza is the right answer but in some miscellaneous situation in my case
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
MaterialPageRoute(builder: (_) => MyScaffold());
break;
case '/homechart':
MaterialPageRoute(builder: (_) => analytics());
break;
}
}
}
I forget to return keyword and IDE not showing any error. showing this kind of error.
Solution is like this :
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => MyScaffold());
break;
case '/homechart':
return MaterialPageRoute(builder: (_) => analytics());
break;
}
}
}
Solution 8 - Flutter
You have instantiated two MaterialApp widget. You need to remove the one in MyApp class.
For push
Navigator.of(context).pushNamed('/screen1')
For Pop: "popAndPushNamed"
Navigator.of(context).popAndPushNamed('/screen4');
Solution 9 - Flutter
I have encountered the same error and solved by just restarting the app
Solution 10 - Flutter
In my case the error was because i deleted the route
from main.dart
and forgot the update the change in other files
Solution 11 - Flutter
I had the same error i don't know why flutter does that after creating a new screen but all you need is rebuild zor re-run the app again
Solution 12 - Flutter
There need to be only one MaterialApp
if you have multiple MaterialApps then I would recommend to keep only one MaterialApp
for your project.
Solution 13 - Flutter
I had the same problem. the problem was that I had a template and had duplicated the screens and didn't delete my materialapps after everything worked tanks a lot
Solution 14 - Flutter
I recently had the same problem any way I managed to resolve it by using Global Key
the context which I need to navigate from was the problem because it refers to a widget not a page that holds this widget so I made static GlobalKey appKey = GlobalKey();
then use it in the scaffold of the page then i can use its context from any place in the app MainPage.appKey.currentContext;
Solution 15 - Flutter
One of the solutions would be hot restarting your code. That is because we declare the route name outside the Widget Build.
Solution 16 - Flutter
If you don't have a home property defined for your MaterialApp, you probably should have this kind of error. Please try to fill it with a Scaffold widget.