I am using authStateChange as a stream in my Splash Screen to check for any changes in the authentication state of my app, but after signing out using Firebase Auth signOut build in function, when I sign in back with a different user, it still shows the data of the previous user, fetching it from the firestore. But when I hot restart the app debugging session, it starts displaying the data of the current signed in user. I have printed the user id many times in the StreamBuilder of the authStateChange, and unless I restart the app, it prints the previous users userID. What could be the issue that forces me to restart the app to get correct data?
Here is my Splash Screen code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:prac_crud/screens/homepage.dart';
import 'package:prac_crud/screens/login.dart';
final FirebaseAuth auth = FirebaseAuth.instance;
final User user = auth.currentUser!;
final uid = user.uid;
class Splash extends StatefulWidget {
const Splash({super.key});
@override
State<Splash> createState() {
return _Splash();
}
}
class _Splash extends State<Splash> {
@override
void initState() {
super.initState();
navigatetoNext();
}
void navigatetoNext() {
Timer(const Duration(seconds: 3), () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) {
return StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
print("User is: $uid");
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasData) {
return const HomePage();
}
return const Login();
},
);
},
),
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Image.network(
// 'https://images.ctfassets.net/4cd45et68cgf/7LrExJ6PAj6MSIPkDyCO86/542b1dfabbf3959908f69be546879952/Netflix-Brand-Logo.png',
// width: 230,
// ),
Image.asset(
"assets/images/pngwing.com.png",
width: 230,
),
const SizedBox(height: 24),
const CircularProgressIndicator(
color: Colors.red,
),
],
),
),
);
}
}
I have tried printing the user ids to check the current user, still shows the previous users ID, unless restarted. Tried to reinitialize the database instance after the sign out, removed the splash and directly used StreamBuilder in the main.dart file, still no results.
Ahmed Ikram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.