You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Android端Firebase电话认证用户状态无法持久化求助

Fixing Firebase Phone Auth Persistence Issue in Flutter

Let's break down the issues in your code and walk through the fixes to resolve the "user is null on app restart" problem:

1. Missing Instance Variables

First, your PhoneAuth class uses currentUser and uid but they aren't declared as instance variables. Add these at the top of the class:

class PhoneAuth with ChangeNotifier {
  String verificationId;
  final FirebaseAuth phoneAuth = FirebaseAuth.instance;
  PhoneAuthStatus phoneAuthStatus = PhoneAuthStatus.SignedOut;
  FirebaseUser currentUser; // Add this
  String uid; // Add this
  // ... rest of your code
}

2. Relying on a Single currentUser() Call

Firebase Auth may not have finished loading the saved user session when your init() method runs. Instead of a one-time check, listen to auth state changes to catch when the session is restored:

Future<void> init() async {
  // Listen for ongoing auth state changes (critical for persistence)
  phoneAuth.onAuthStateChanged.listen((FirebaseUser user) {
    currentUser = user;
    if (user == null) {
      phoneAuthStatus = PhoneAuthStatus.SignedOut;
    } else {
      uid = user.uid;
      phoneAuthStatus = PhoneAuthStatus.SignedIn;
    }
    notifyListeners(); // Update UI whenever state changes
  });

  // Initial check for current user
  currentUser = await phoneAuth.currentUser();
  if (currentUser == null) {
    phoneAuthStatus = PhoneAuthStatus.SignedOut;
  } else {
    uid = currentUser.uid;
    phoneAuthStatus = PhoneAuthStatus.SignedIn;
  }
  notifyListeners(); // Don't forget to notify here too!
}

3. Missing notifyListeners() in Signed-In Path

In your original init() method, you didn't call notifyListeners() when currentUser was not null. This meant the UI never received the signed-in status update even if the user was authenticated. The fix above adds this call.

4. Incorrect Provider Initialization

You're using ChangeNotifierProvider.value to create your PhoneAuth instance, which is meant for existing objects. Use create instead to properly initialize the provider:

MultiProvider(
  providers: [
    ChangeNotifierProvider(create: (_) => PhoneAuth.initialize()), // Use create instead of value
  ],
  // ... rest of your MaterialApp code
)

Why This Works

  • The onAuthStateChanged listener will trigger every time the auth state changes, including when Firebase finishes loading the saved user session after app restart.
  • Properly notifying listeners ensures the UI updates to reflect the correct auth status.
  • Using create in the provider ensures the PhoneAuth instance is initialized correctly and managed throughout the app lifecycle.

After applying these changes, your app should retain the user session across restarts and correctly show the signed-in state without requiring re-login.

内容的提问来源于stack exchange,提问作者Preet Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 18:42:50