使用路由时在空值上使用的空检查运算符
Null check operator used on a null value when using routes
我正在重构我的应用程序以使用 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,并且我已经尝试了以下操作:
- 运行 Android 版本,看看是否发生同样的情况:大致相同的情况发生,只是 material 包抛出错误而不是 cupertino
- 用静态文本替换本地化标题,但结果是一样的
看来我遗漏了一些明显的东西(我是 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!
@puelo 的评论帮助我解决了这个问题。
问题是在初始化过程的早期我初始化了 Firebase 应用程序,并且根据结果我提供了不同的应用程序小部件。如果 firebase 仍在加载或抛出错误,我展示了一个简单的应用程序,其中设置了 home
属性 - 这就是问题所在。现在我更改了它,以便应用程序始终相同,但加载和错误状态是通过导航来处理的。
我正在重构我的应用程序以使用 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,并且我已经尝试了以下操作:
- 运行 Android 版本,看看是否发生同样的情况:大致相同的情况发生,只是 material 包抛出错误而不是 cupertino
- 用静态文本替换本地化标题,但结果是一样的
看来我遗漏了一些明显的东西(我是 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!
@puelo 的评论帮助我解决了这个问题。
问题是在初始化过程的早期我初始化了 Firebase 应用程序,并且根据结果我提供了不同的应用程序小部件。如果 firebase 仍在加载或抛出错误,我展示了一个简单的应用程序,其中设置了 home
属性 - 这就是问题所在。现在我更改了它,以便应用程序始终相同,但加载和错误状态是通过导航来处理的。