Null check operator used on a null value when using routes


I’m refactoring my app to support localizations using this guide. At one point I need to switch from using home property to using initialRoute and routes. The relevant code fragment looks like this:

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

This generates the following error when I try to run this on 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/
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<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/routes.dart:840:53)
#4 (package:flutter/src/widgets/basic.dart:7555:48)

I use the flutter_platform_widgets for this app and I tried the following things already:

  • Run the Android version and see if the same happens: roughly the same happens, just the material package throws the error instead of cupertino
  • Replace the localized title with the static text, but the result is the same

It looks like I’m missing something obvious (I’m new to Flutter). When I comment out the initialRoute and routes and use home instead, it works ok – but obviously without the localizations. So, what am I missing here?

Flutter doctor output:

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!


The comment from @puelo helped me to figure this out.

The problem was that earlier in the init process I init the Firebase app, and depending on the outcome I provided different app widget. If the firebase was still loading or threw an error, I presented a simple app with the home property set – this was the problem. Now I changed it so that the app is always the same but the loading and error state are handled with a navigation instead.

Answered By – lawicko

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *