Getting this error with this code:
void main() => runApp(RouteTestApp());
class RouteTestApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
home: FirstScreen(),
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
);
}
}
The following assertion was thrown building MaterialApp(dirty, state: _MaterialAppState#a959e): I/flutter (24918): If the home property is specified, the routes table cannot include an entry for "/", since it would I/flutter (24918): be redundant. I/flutter (24918): 'package:flutter/src/widgets/app.dart': I/flutter (24918): Failed assertion: line 172 pos 10: 'home == null || I/flutter (24918): !routes.containsKey(Navigator.defaultRouteName)'
home: This property takes in widget as the object to show on the default route of the app. initialRoute: This property takes in a string as the object to give the name of the first route in which the navigator is built. locale: It provides a locale for the MaterialApp.
To solve this problem, we need to use a different context. In this situation, the easiest solution is to introduce a new widget as child of MaterialApp. And then use that widget's context to do the Navigator call. That's All.
The solution is to remove the home property, since it can cause problems if you add the routes property.
class RouteTestApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Demo',
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With