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

Viewed 6532

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

2 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(),
      },
    );
  }
}

If you set none as '/' code bellow will help you when test widgets with navigation, and still work.

final routes = <String, WidgetBuilder>{
  '/one': (BuildContext context) => PageOne(),
  '/two': (BuildContext context) => PageTwo(),
  ...
};

runApp(MaterialApp(initialRoute: '/one', routes: appRoutes));

Related