Skip to content

🐛 [firebase_auth] FirebaseAuth.instance.currentUser initially null for the first few seconds initializing firebase on windows #12055

Open
@Justus-M

Description

@Justus-M

Bug report

Describe the bug

FirebaseAuth.instance.currentUser is always null when initializing firebase on windows, even if the user is logged in. It then changes to a non-null value within a few seconds.

Steps to reproduce

Steps to reproduce the behavior:

  1. Login with FirebaseAuth on flutter windows
  2. restart app and print FirebaseAuth.instance.currentUser immediately after initializing firebase
  3. See that the value is null initially, and then changes after about 20 seconds

Expected behavior

I expect the logged in user object to be shown on initialization, otherwise I have to add a delay (not sure how long it needs to be) or I don't know whether to show the login page or not. On mac and web, currentUser is immediately loaded on initialization.

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
PS C:\Users\justinmulli\blazesql_flutter> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.16.4, on Microsoft Windows [Version 10.0.17763.5122], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[X] Android toolchain - develop for Android devices
    X Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.2)
[!] Android Studio (not installed)
[√] VS Code (version 1.85.1)
[√] Connected device (3 available)
[√] Network resources

! Doctor found issues in 2 categories.
PS C:\Users\justinmulli\blazesql_flutter>```


---

Activity

changed the title [-]🐛 [firebase_auth] FirebaseAuth.instance.currentUser initially null after initializing firebase on windows, even for logged in users[/-] [+]🐛 [firebase_auth] FirebaseAuth.instance.currentUser initially null for the first few seconds initializing firebase on windows[/+] on Dec 19, 2023
danagbemava-nc

danagbemava-nc commented on Dec 19, 2023

@danagbemava-nc

Hi @Justus-M, is this limited to only windows or do you experience it on other platforms as well?

Can you share the code sample you use as well?

added
blocked: customer-responseWaiting for customer response, e.g. more information was requested.
and removed
Needs AttentionThis issue needs maintainer attention.
on Dec 19, 2023
Justus-M

Justus-M commented on Dec 23, 2023

@Justus-M
Author

I haven't had time to create a minimum reproducible example but I imagine you can just use the example app and print FirebaseAuth.instance.currentUser immediately after initializing firebase.

I have added a delay of a few seconds and then it is no longer null, so to be honest this is no longer a priority for me. I believe it's an issue with the library so I wanted to bring it to your attention since flutterfire is still in the early stages for windows and has various issues.

But the code is literally just

print(FirebaseAuth.instance.currentUser)

This prints null on windows after initializing firebase, even if the user was logged in before closing the app.

On mac, web, and mobile it does not return null

google-oss-bot

google-oss-bot commented on Jan 2, 2024

@google-oss-bot

Hey @Justus-M. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

Justus-M

Justus-M commented on Jan 2, 2024

@Justus-M
Author

I provided more information

added
Needs AttentionThis issue needs maintainer attention.
and removed
blocked: customer-responseWaiting for customer response, e.g. more information was requested.
StaleIssue with no recent activity
on Jan 2, 2024

20 remaining items

remcova

remcova commented on Aug 6, 2024

@remcova

No updates as far as I'm aware, except that I lost several customers because of it

Yeah same here. I'm trying to find a solution at the moment. I'll post it here once I find it.

remcova

remcova commented on Aug 8, 2024

@remcova

@Justus-M do you use a YouTube scope by any chance for authentication?

Justus-M

Justus-M commented on Aug 8, 2024

@Justus-M
Author
remcova

remcova commented on Aug 8, 2024

@remcova

Okay. I think I found a solution. It's the way you handle authentication. My current implementation that works now, looks like this:
await FirebaseAuth.instance.signInWithCredential(credential).whenComplete(() async { final user = FirebaseAuth.instance.currentUser; // do something with user });

Before I did it like this:
FirebaseAuth.instance.signInWithCredential(credential); FirebaseAuth.instance.authStateChanges().listen((User? user) async { // do something with the user object }

I hope this solves the issue for you too!

No, I had users encountering this issue with a normal email and password login

On Thu 8. Aug 2024 at 16:43, Remco van Akker @.> wrote: @Justus-M https://github.com/Justus-M do you use a YouTube scope by any chance for authentication? — Reply to this email directly, view it on GitHub <#12055 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI . You are receiving this because you were mentioned.Message ID: @.>

Justus-M

Justus-M commented on Aug 8, 2024

@Justus-M
Author
BirjuVachhani

BirjuVachhani commented on Aug 10, 2024

@BirjuVachhani

This issue makes Firebase useless because Windows app is un-usable. Can someone please look into this? @Lyokone

venky9885

venky9885 commented on Aug 13, 2024

@venky9885

having same issue

remcova

remcova commented on Aug 13, 2024

@remcova

How do you handle the authentication then? I'm saying that I'm not experiencing this issue anymore.

I was never adding a listener in the first place, your issue probably wasn’t network related like everyone else in this thread from the sounds of it On Thu 8. Aug 2024 at 18:03, Remco van Akker @.> wrote:

Okay. I think I found a solution. It's the way you handle authentication. My current implementation that works now, looks like this: await FirebaseAuth.instance.signInWithCredential(credential).whenComplete(() async { final user = FirebaseAuth.instance.currentUser; // do something with user }); Before I did it like this: FirebaseAuth.instance.signInWithCredential(credential); FirebaseAuth.instance.authStateChanges().listen((User? user) async { // do something with the user object } I hope this solves the issue for you too! No, I had users encountering this issue with a normal email and password login … <#m_5950327318018341650_> On Thu 8. Aug 2024 at 16:43, Remco van Akker @.
> wrote: @Justus-M https://github.com/Justus-M https://github.com/Justus-M https://github.com/Justus-M do you use a YouTube scope by any chance for authentication? — Reply to this email directly, view it on GitHub <#12055 (comment) <#12055 (comment)>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI https://github.com/notifications/unsubscribe-auth/AI2UQLB5IIHR5BTRT5Q45LTZQN72LAVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGAYDONZSGI . You are receiving this because you were mentioned.Message ID: @.
> — Reply to this email directly, view it on GitHub <#12055 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2UQLHZJZ7XYWGRBS7YWWLZQOJF7AVCNFSM6AAAAABA22MTZWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZWGE3TOOJXHE . You are receiving this because you were mentioned.Message ID: @.
**>

ronpetit

ronpetit commented on Sep 22, 2024

@ronpetit

I'm having this error too, seems related to the channel error as it looks relevant to the auth-state

[ERROR:flutter/shell/common/shell.cc(1015)] The 'firebase_auth_plugin/auth-state/[DEFAULT]' channel sent a message from native to Flutter on a non-platform thread. Platform channel messages must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must be fixed in the plugin or application code creating that channel.
See https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading for more information.

flutter 3.24.3
firebase_core 3.4.1
firebase_auth 5.2.1

colonal

colonal commented on Dec 7, 2024

@colonal

I faced a similar issue where FirebaseAuth.instance.currentUser initially returned null for the first few seconds during Firebase initialization. Here's what I did to effectively handle this problem:

Solution

I utilized the FirebaseAuth.instance.authStateChanges() stream to listen for authentication state changes and created a FirebaseAuthService class to manage this behavior.

FirebaseAuthService Class

class FirebaseAuthService{
    static bool isLoggedIn = false;
    
    static void authStateChanges() {
        FirebaseAuth.instance.authStateChanges().listen((event) {
          if (event != null) {
            isLoggedIn = true;
          } else {
            isLoggedIn = false;
          }
        });
    }
}

Main File

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  FirebaseAuthService.authStateChanges();
  runApp(const FruitHub());
}

Example: Checking Login Status in the Splash Screen

From any file, you can check if the user is logged in. For example, in the splash screen:

var isLoggedIn = FirebaseAuthService.isLoggedIn;
if (isLoggedIn) {
  Navigator.pushReplacementNamed(context, MainView.routeName);
} else {
  Navigator.pushReplacementNamed(context, SignInView.routeName);
}

This approach ensures that your app correctly detects the user's authentication state as soon as it's available and navigates appropriately.

I hope this helps!

BirjuVachhani

BirjuVachhani commented on Dec 8, 2024

@BirjuVachhani

@Lyokone Any updates on this?

tortiolaware

tortiolaware commented on May 12, 2025

@tortiolaware

Any update on this? Are the team continuing developing flutter-firebase for windows?

BirjuVachhani

BirjuVachhani commented on May 12, 2025

@BirjuVachhani

Seems abandoned! 😢

KKimj

KKimj commented on May 28, 2025

@KKimj
Contributor
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

// Add this before runApp() to ensure auth state initialization
// This waits for the initial auth event before proceeding
await FirebaseAuth.instance.authStateChanges().first; 

// runApp(); ...

It seems that authStateChanges() may emit null initially during Firebase initialization, possibly before the actual auth state is determined.

Using await authStateChanges().first might help ensure the app waits for the correct auth state, potentially avoiding a premature null from currentUser during initial load.

richard457

richard457 commented on May 29, 2025

@richard457

I am afraid Google will kill this soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @Lyokone@ronpetit@remcova@richard457@BirjuVachhani

    Issue actions

      🐛 [firebase_auth] FirebaseAuth.instance.currentUser initially null for the first few seconds initializing firebase on windows · Issue #12055 · firebase/flutterfire