The authStateChange is not updating the authentication state, unless I resart the app deugging session, causing previous user data to be displayed

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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,
),
],
),
),
);
}
}
</code>
<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, ), ], ), ), ); } } </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.

New contributor

Ahmed Ikram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật