Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if the home property is specified the routes table cannot include an entry for /

Tags:

flutter

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)'

like image 856
live-love Avatar asked Apr 11 '20 13:04

live-love


People also ask

What is home in MaterialApp flutter?

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.

How do you fix navigator operation requested with a context that does not include a Navigator?

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.


1 Answers

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(),
      },
    );
  }
}
like image 176
live-love Avatar answered Sep 23 '22 11:09

live-love