我正在重构我的应用以支持使用 this guide 的本地化。有一次我需要从使用 home
属性切换到使用 initialRoute
和 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,并且已经尝试了以下操作:
看起来我遗漏了一些明显的东西(我是 Flutter 的新手)。当我注释掉 initialRoute
和 routes
并使用 home
时,它工作正常 - 但显然没有本地化。那么,我在这里错过了什么?
Flutter 医生输出:
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!
答案 0 :(得分:0)
@puelo 的评论帮助我解决了这个问题。
问题是在初始化过程的早期我初始化了 Firebase 应用程序,并根据结果我提供了不同的应用程序小部件。如果 Firebase 仍在加载或引发错误,我会展示一个带有 home
属性集的简单应用程序 - 这就是问题所在。现在我更改了它,以便应用程序始终相同,但加载和错误状态由导航处理。