I know there are a lot of similar questions already out there, but none of the answers seem to resolve my problem.

I get the error Null check operator used on a null value, however I don't use the bang operator anywhere in my code.

Here's my main.dart file:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:firebase_core/firebase_core.dart';
import 'package:todo/screens/home.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const App());
}

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initialization,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return MaterialApp( // ERROR CAUSING WIDGET
            routes: {
              '/': (context) => const HomeScreen()
            },
            // home: const HomeScreen(),  NO ERROR!
          );
        }
      },
    );
  }
}

The home.dart file looks like this:

import 'package:flutter/material.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('HOME'),
      ),
    );
  }
}

The problem only occurs when using routes, when I assign HomeScreen() directly to the home property of MaterialApp, everything works fine.

Also note that everything worked fine before I added Firebase and the FutureBuilder.

Edit: I shortened the above code for better readability. I actually use the FutureBuilder to differentiate between multiple screens (a loading screen, an error screen and the home screen), depending on the future's state.

Here's the full stacktrace:

The relevant error-causing widget was: 
  MaterialApp MaterialApp:file:///D:/Code/Workspace/Flutter/todo/lib/main.dart:43:18
When the exception was thrown, this was the stack: 
#0      _WidgetsAppState._onGenerateRoute.<anonymous closure> (package:flutter/src/widgets/app.dart:1350:48)
#1      MaterialPageRoute.buildContent (package:flutter/src/material/page.dart:53:55)
#2      MaterialRouteTransitionMixin.buildPage (package:flutter/src/material/page.dart:106:27)
#3      _ModalScopeState.build.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/routes.dart:872:55)
#4      Builder.build (package:flutter/src/widgets/basic.dart:7398:48)
#5      StatelessElement.build (package:flutter/src/widgets/framework.dart:4827:28)
#6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#7      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#8      StatelessElement.update (package:flutter/src/widgets/framework.dart:4834:5)
#9      Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#10     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#11     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#12     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#13     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#14     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#15     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#16     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#17     StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#18     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#19     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#20     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#21     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#22     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#23     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#24     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#25     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#26     StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#27     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#28     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#29     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#30     StatelessElement.update (package:flutter/src/widgets/framework.dart:4834:5)
#31     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#32     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#33     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#34     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#35     StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#36     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#37     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#38     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#39     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#40     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#41     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#42     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#43     ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#44     _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:111:11)
#45     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#46     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#47     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#48     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#49     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#50     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#51     StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#52     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#53     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#54     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#55     ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#56     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#57     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#58     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#59     ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#60     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#61     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#62     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#63     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#64     StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#65     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#66     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#67     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#68     StatelessElement.update (package:flutter/src/widgets/framework.dart:4834:5)
#69     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#70     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#71     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#72     StatelessElement.update (package:flutter/src/widgets/framework.dart:4834:5)
#73     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#74     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6291:14)
#75     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#76     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#77     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#78     ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#79     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#80     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#81     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#82     ProxyElement.update (package:flutter/src/widgets/framework.dart:5108:5)
#83     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#84     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#85     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#86     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#87     StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#88     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#89     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#90     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#91     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#92     StatefulElement.update (package:flutter/src/widgets/framework.dart:4960:5)
#93     Element.updateChild (package:flutter/src/widgets/framework.dart:3501:15)
#94     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4780:16)
#95     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#96     Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#97     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
#98     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#99     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#100    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#101    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
#102    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
#106    _invoke (dart:ui/hooks.dart:151:10)
#107    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#108    _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)


Solution 1: Narayan Poudel

If you are using the future just for firebase initialization, you can simply try this one.

 void main() async {
   WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp();
   runApp(const MyApp());
 }


Solution 2: CoderSteve

When you use Named Routes in your Flutter app you need to provide the "initialRoute" attribute for the MaterialApp widget.

In your case add the following line just before the "routes" attribute:

    initialRoute: '/',

so your MaterialApp code will look like this:

    return MaterialApp( 
       initialRoute: '/',
       routes: {
         '/': (context) => const HomeScreen()
       },            
    );

Note: You must remove the "home" attribute of MaterialApp as you cannot have both the "home" and "initialRoute" attributes being set at the same time.