0

我正在使用本指南重构我的应用程序以支持本地化。在某一时刻,我需要从 using homeproperty 切换到 using initialRouteand routes。相关的代码片段如下所示:

return PlatformApp(
            material: (_, platform) => MaterialAppData(theme: materialThemeData),
            cupertino: (_, platform) => CupertinoAppData(theme: cupertinoTheme),
            onGenerateTitle: (context) {
              return AppLocalizations.of(context).appTitle;
            },
            localizationsDelegates: [
              AppLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            supportedLocales: [
              const Locale('de', ''),
              const Locale('en', ''),
            ],
            initialRoute: '/',
            routes: {
              '/': (context) {
                return MyHomeScreen(title: AppLocalizations.of(context).appTitle);
              },
            },
            // home:MyHomeScreen(title: 'Static Title'),
          );
        }

当我尝试在 iOS 上运行它时,这会产生以下错误:

======== Exception caught by widgets library =======================================================
The following _CastError was thrown building Builder(dirty):
Null check operator used on a null value

The relevant error-causing widget was: 
  CupertinoApp file:///Users/user/Development/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_platform_widgets-1.7.1/lib/src/platform_app.dart:673:14
When the exception was thrown, this was the stack: 
#0      _WidgetsAppState._onGenerateRoute.<anonymous closure> (package:flutter/src/widgets/app.dart:1196:48)
#1      CupertinoPageRoute.buildContent (package:flutter/src/cupertino/route.dart:359:55)
#2      CupertinoRouteTransitionMixin.buildPage (package:flutter/src/cupertino/route.dart:227:26)
#3      _ModalScopeState.build.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/routes.dart:840:53)
#4      Builder.build (package:flutter/src/widgets/basic.dart:7555:48)
...
====================================================================================================

我为此应用程序使用了flutter_platform_widgets ,并且我已经尝试了以下操作:

  • 运行Android版本,看看是否出现同样的情况:大致相同,只是材料包抛出错误而不是cupertino
  • 用静态文本替换本地化的标题,但结果是一样的

看起来我遗漏了一些明显的东西(我是 Flutter 的新手)。当我注释掉initialRouteandrouteshome改为使用时,它可以正常工作 - 但显然没有本地化。那么,我在这里缺少什么?

颤振医生输出:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.6, on macOS 11.2.3 20D91 darwin-x64, locale en-CH)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.2)
[✓] Connected device (4 available)

• No issues found!
4

1 回答 1

0

@puelo 的评论帮助我解决了这个问题。

问题是在初始化过程的早期我初始化了 Firebase 应用程序,并且根据结果我提供了不同的应用程序小部件。如果 firebase 仍在加载或抛出错误,我提供了一个带有home属性集的简单应用程序 - 这就是问题所在。现在我对其进行了更改,以便应用程序始终相同,但加载和错误状态由导航处理。

于 2021-05-25T09:25:14.637 回答