如果指定了 home 属性,则路由表不能包含 / 的条目

liv*_*ove 5 flutter

使用此代码获取此错误:

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(),
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

以下断言被抛出 building MaterialApp(dirty, state: _MaterialAppState#a959e): I/flutter (24918): 如果指定了 home 属性,路由表不能包含“/”的条目,因为它会 I/flutter ( 24918):是多余的。I/flutter (24918): 'package:flutter/src/widgets/app.dart': I/flutter (24918): 失败的断言: line 172 pos 10: 'home == null || I/flutter (24918): !routes.containsKey(Navigator.defaultRouteName)'

liv*_*ove 13

解决方案是删除home属性,因为如果添加routes属性会导致问题。

class RouteTestApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      initialRoute: '/',
      routes: {
        '/': (context) => FirstScreen(),
        '/second': (context) => SecondScreen(),
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)