简体   繁体   中英

Flutter "null check operator used on a null value" while using routes in MaterialApp

My main.dart file

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

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

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

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      // Initialize FlutterFire:
      future: _initialization,
      builder: (context, snapshot) {
        // Check for errors
        if (snapshot.hasError) {
          // ... show error page ...
          // → this does work
        }

        // Once complete, show your application
        if (snapshot.connectionState == ConnectionState.done) {

// my problem is somewhere inside this MaterialApp

          return MaterialApp(
            initialRoute: Navigator.defaultRouteName, // equals to '/'
            routes: appRoutes,
          );
        }

        // Otherwise, show something whilst waiting for initialization to complete
        // ... show loading page ...
        // → this does work
      },
    );
  }
}

my routes(appRoutes) definition (imported from another file)

var appRoutes = {
  Navigator.defaultRouteName: (context) => HomeScreen(),
  '/login': (context) => LoginScreen(),
  '/about': (context) =>  AboutScreen(),
  '/profile': (context) =>  ProfileScreen(),
  '/topics': (context) =>  TopicsScreen(),
};

As long as I just return a simple MaterialApp with some content, it does work fine. But the moment I add the routes -keyword (instead of the content) there is the null check operator error.

I did find this post, where there was a very similar problem but the solution didn't work for me: Flutter: MaterialApp throws Null check operator used error

The pages are very simple MaterialApps or Scaffolds just showing a Text

As an example: my HomeScreen() build widget

Widget build(BuildContext context) {
  return MaterialApp( // replacing this with a Scaffold didn't solve my problem either
    home: Container(
      child: Text(
        'This works',
        style: TextStyle(fontSize: 30,),
      ),
    ),
  );
}

I have a few outdated transitive dependencies (but flutter pub upgrade doesn't change anything - due to dependency constraints)

transitive dependencies: 
async                     *2.8.2    *2.8.2      *2.8.2      2.9.0    
characters                *1.2.0    *1.2.0      *1.2.0      1.2.1    
clock                     *1.1.0    *1.1.0      *1.1.0      1.1.1    
fake_async                *1.3.0    *1.3.0      *1.3.0      1.3.1    
matcher                   *0.12.11  *0.12.11    *0.12.11    0.12.12  
material_color_utilities  *0.1.4    *0.1.4      *0.1.4      0.1.5    
meta                      *1.7.0    *1.7.0      *1.7.0      1.8.0    
path                      *1.8.1    *1.8.1      *1.8.1      1.8.2    
source_span               *1.8.2    *1.8.2      *1.8.2      1.9.1    
string_scanner            *1.1.0    *1.1.0      *1.1.0      1.1.1    
term_glyph                *1.2.0    *1.2.0      *1.2.0      1.2.1    
test_api                  *0.4.9    *0.4.9      *0.4.9      0.4.12

My previous research suggests that the problem is caused by the routes. What am I doing wrong?


EDIT: Maybe it does matter, that I have connected my phone via USB to test the app. (Emulator is eating up too much RAM)


EDIT: I now modified the code, so that Linux and Web builds are also able to test the routes.

Same problem here.

Have you tried removing the futurebuilder just to initialize the firebase?

You can initialize it in the main function itself as an async function

 void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(App()); } class App extends StatefulWidget { const App({super.key}); @override State<App> createState() => _AppState(); } class _AppState extends State<App> { @override Widget build(BuildContext context) { return MaterialApp( initialRoute: Navigator.defaultRouteName, // equals to '/' routes: appRoutes, ); } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM