Reputation: 41
I am trying to change screens using the value of a string inside a json object but I get the following error in the console and it does not change my screen:
Could not find a generator for route RouteSettings ("alert", null) in the _WidgetsAppState.
Generators for routes are searched for in the following order:
- For the "/" route, the "home" property, if non-null, is used.
- Otherwise, the "routes" table is used, if it has an entry for the route.
- Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
- Finally if all else fails onUnknownRoute is called.
Unfortunately, onUnknownRoute was not set.
When the exception was thrown, this was the stack
This is my Home Page, the method is working until Navigator.pushNamed(context, opt['ruta']);:
List<Widget> _listItems(List<dynamic> data, BuildContext context) {
final List<Widget> options = [];
data.forEach((opt){
final widgetTemp = ListTile(
title: Text(opt['texto']),
leading: getIcon(opt['icon']),
trailing: Icon(Icons.keyboard_arrow_right, color: Colors.blue,),
onTap: (){
print("Hello");
Navigator.pushNamed(context, opt['ruta']);
},
);
options..add(widgetTemp)
..add(Divider());
});
return options;
}
And this is my main.dart where I have the routes:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Components App',
debugShowCheckedModeBanner: false,
initialRoute: '/',
routes: <String, WidgetBuilder>{
'/' : (BuildContext context) => HomePage(),
'/alert' : (BuildContext context) => AlertPage(),
'/avatar' : (BuildContext context) => AvatarPage(),
},
onGenerateRoute: (settings){
print("Ruta llamada: ${settings.name }");
},
);
}
}
Upvotes: 0
Views: 2145
Reputation: 46
The right answer right now is:
Navigator.of(context,
rootNavigator: true).pushNamed('/${element['ruta']}');
Upvotes: 0
Reputation: 378
To prevent this kind of errors always write your route names in a separated file like constants of something. That way you sure that every route name used in pushNamed is equal to what onGenerateRoute has.
Upvotes: 0
Reputation: 126894
You forgot to add the leading /
:
Navigator.pushNamed(context, '/${opt['ruta']}');
The route name needs to match the generators exactly.
Upvotes: 1