Null check operator used on a null value when using Named routes
1,568
Solution 1
I encountered the same situation as you did and relaunched the app solved this problem.
Solution 2
Not sure about the «why» but you have to change the «root» widget from the FutureBuilder to the MaterialApp registering the named routes.
Had the same code structure and the same bug as you. I made my MaterialApp as the argument to runApp
and everything is fine ¯_(ツ)_/¯
Comments
-
Omar Fayad over 1 year
I am getting this error after I switched to
named routes
where my app was working properly when usingMaterialPageRoute
:main.dart:
void main() { WidgetsFlutterBinding.ensureInitialized(); runApp( MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => ClassA()), ChangeNotifierProvider( create: (context) => ClassB()), ], child: MyApp(), ), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return FutureBuilder( future: Firebase.initializeApp(), builder: (context, snapshot) { if (snapshot.hasError) { return ... } if (snapshot.connectionState == ConnectionState.done) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'App Title', initialRoute: '/', routes: { '/': (context) => Wrapper(), 'somePage/': (context) => SomePage(), }, ); } //else ... return ... ; }); } }
The error message:
The following _CastError was thrown building Builder(dirty): Null check operator used on a null value The relevant error-causing widget was: MaterialApp file:///.../lib/main.dart:40:20 When the exception was thrown, this was the stack: #0 _WidgetsAppState._onGenerateRoute.<anonymous closure> (package:flutter/src/widgets/app.dart:1196:48) #1 MaterialPageRoute.buildContent (package:flutter/src/material/page.dart:54:55) #2 MaterialRouteTransitionMixin.buildPage (package:flutter/src/material/page.dart:107:27) #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 Doctor:
Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 2.0.2, on Microsoft Windows [Version 10.0.19042.867], locale en-US) [√] Android toolchain - develop for Android devices (Android SDK version 30.0.1) [√] Chrome - develop for the web [√] Android Studio (version 4.1.0) [√] VS Code (version 1.52.1) [√] Connected device (3 available) • No issues found!
The weird thing is that the app works after multiple hot-restarts, then a hot-restart crash it.