So, I started to make a Flutter App with sdk: ">=2.16.2 <3.0.0" and Firebase Integration. I have the following code on main.dart for "authStateChanges()":
if (snapshot.connectionState == ConnectionState.done) {
FirebaseAuth.instance.authStateChanges().listen((User? user) {
if (user == null) {
Navigator.of(context).pushReplacementNamed('login');
} else {
Navigator.of(context).pushReplacementNamed('home');
}
});
}
It works at the first time when loading the app, but If the user signs in (or out), then the "Unhandled Exception: Null check operator used on a null value" occurs on Navigator.
Here is the full main.dart code:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:pollux_android/src/pages/error_page.dart';
import 'package:pollux_android/src/pages/loading_page.dart';
import 'package:pollux_android/src/routes/routes.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
}
class _MyAppState extends State<MyApp> {
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
routes: polluxRoutes,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: mainScreen(),
);
}
mainScreen() {
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
if (snapshot.hasError) {
return const ErrorPage(
errorMessage: 'Unknown error',
);
}
if (snapshot.connectionState == ConnectionState.done) {
FirebaseAuth.instance.authStateChanges().listen((User? user) {
if (user == null) {
Navigator.of(context).pushReplacementNamed('login');
} else {
Navigator.of(context).pushReplacementNamed('home');
}
});
}
return const LoadingPage();
},
);
}
}
I don't get why it fails to Navigate the second time on authStateChanges(). I appreciate for the help you can provide. I believe it has to do with the Null check on newer versions as this issue didn't happen on sdk 2.7.0
