为什么 Flutter 尝试渲染“/”而不是我的“initialRoute”?

Why does Flutter try to render '/' rather than my `initialRoute`?

我有一个 Flutter 应用程序,我试图在其中更深入地研究路由。当我在我的 MaterialApp 小部件中使用 home 属性 时,它似乎非常高兴。一旦我将其更改为使用 initialRoute,应用程序就会崩溃并显示 NoSuchMethodError 和一个错误 The builder for route '/' returned null - 我的初始路线是 '/loading'.

错误:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building Builder(dirty):
Closure call with mismatched arguments: function 'new MyApp.<anonymous closure>'
Receiver: Closure: (BuildContext) => LoadingScreen
Tried calling: new MyApp.<anonymous closure>(Instance of 'StatelessElement', null)
Found: new MyApp.<anonymous closure>(BuildContext) => LoadingScreen

The relevant error-causing widget was: 
  MaterialApp file:///Users/tanner/Documents/Coding/Courses/AppBrewery/Flutter/Clima-Flutter/lib/main.dart:18:12
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1      MyApp._buildRoute (package:clima/main.dart:54:30)
#2      MyApp._makeRoute (package:clima/main.dart:43:26)
#3      MyApp._generateRoute.<anonymous closure> (package:clima/main.dart:27:42)
#4      MaterialPageRoute.buildPage (package:flutter/src/material/page.dart:87:27)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The builder for route "/" returned null.
The relevant error-causing widget was: 
  MaterialApp file:///Users/tanner/Documents/Coding/Courses/AppBrewery/Flutter/Clima-Flutter/lib/main.dart:18:12
════════════════════════════════════════════════════════════════════════════════════════════════════

我的路由怪癖

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final Map<String, Function> _routes = {
    '/loading': (BuildContext context, LoadingScreenArguments args) =>
        LoadingScreen(args),
    '/location': (BuildContext context, LocationScreenArguments args) =>
        LocationScreen(args),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
//      home: LoadingScreen(),
      initialRoute: '/loading',
      onGenerateRoute: _generateRoute,
    );
  }

  MaterialPageRoute _generateRoute(RouteSettings settings) {
    return MaterialPageRoute(
      builder: (BuildContext context) => _makeRoute(
        context: context,
        routeName: settings.name,
        arguments: settings.arguments,
      ),
      maintainState: true,
      fullscreenDialog: false,
    );
  }

  Widget _makeRoute({
    @required BuildContext context,
    @required String routeName,
    Object arguments,
  }) {
    final Widget child = _buildRoute(
        context: context, routeName: routeName, arguments: arguments);
    return child;
  }

  Widget _buildRoute({
    @required BuildContext context,
    @required String routeName,
    Object arguments,
  }) {
    print(routeName);
    return _routes[routeName](context, arguments);
  }
}

这是因为它找不到任何名为“/loading”的路线,因为您制作了 <String, Function>route 地图,而路线是 WidgetBuilder 的。

所以尝试替换:

Map<String, Function>

与:

Map<String, WidgetBuilder>

感谢@pskink 对我的回答的评论,解决方案非常明显:删除前导 / 解决了问题。

源代码中的 comment 显示 Navigator 将字符串拆分为 / 个字符并为每个块加载路由,以保留可追溯的历史记录。