Android Flatter Firebase auth用户在设备上不是持久的

Android Flatter Firebase auth用户在设备上不是持久的,android,firebase,flutter,firebase-authentication,Android,Firebase,Flutter,Firebase Authentication,我在android的Flitter应用程序中使用firebase phone auth。登录系统工作正常。但是,每次我打开应用程序时,它都会返回null user。因此,我必须在每次打开应用程序时登录。我不知道这是我这边的一个bug还是一个错误,但我会在这里和你们分享代码 PhoneAuth代码: enum PhoneAuthStatus { SignedIn, CodeSent, HasError, SignedOut, } class PhoneAuth with Cha

我在android的Flitter应用程序中使用firebase phone auth。登录系统工作正常。但是,每次我打开应用程序时,它都会返回null user。因此,我必须在每次打开应用程序时登录。我不知道这是我这边的一个bug还是一个错误,但我会在这里和你们分享代码

PhoneAuth代码:

enum PhoneAuthStatus {
  SignedIn,
  CodeSent,
  HasError,
  SignedOut,
}


class PhoneAuth with ChangeNotifier {
  String verificationId;
  final FirebaseAuth phoneAuth = FirebaseAuth.instance;
  PhoneAuthStatus phoneAuthStatus = PhoneAuthStatus.SignedOut;

  PhoneAuth.initialize() {
    init();
  }

  Future<void> init() async {
    currentUser = await phoneAuth.currentUser();
    if (currentUser == null) {
      phoneAuthStatus = PhoneAuthStatus.SignedOut;
      notifyListeners();
    } else {
      phoneAuthStatus = PhoneAuthStatus.SignedIn;
    }
  }

  PhoneAuthStatus get status => phoneAuthStatus;
  FirebaseUser get user => currentUser;

  Future<void> verifyPhoneNumber(String phNum) async {
    phoneAuth.verifyPhoneNumber(
        phoneNumber: phNum,
        timeout: Duration(seconds: 90),
        verificationCompleted: (AuthCredential phoneAuthCredential) async {
          AuthResult authResult;
          try {
            authResult =
                await phoneAuth.signInWithCredential(phoneAuthCredential);
          } catch (error) {
            phoneAuthStatus = PhoneAuthStatus.HasError;
            notifyListeners();
            return;
          }
          currentUser = authResult.user;
          uid = currentUser.uid;
          phoneAuthStatus = PhoneAuthStatus.SignedIn;
          notifyListeners();
        },
        verificationFailed: (AuthException authException) {
          print(authException.message);
          phoneAuthStatus = (PhoneAuthStatus.HasError);
          notifyListeners();
        },
        codeSent: (String vId, [int forceResendingToken]) async {
          phoneAuthStatus = (PhoneAuthStatus.CodeSent);
          notifyListeners();
          verificationId = vId;
        },
        codeAutoRetrievalTimeout: (String vId) {
          verificationId = vId;
        });
    return;
  }

  Future<void> signInWithPhoneNumber(String code) async {
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: verificationId,
      smsCode: code.toString(),
    );
    AuthResult authResult;
    try {
      authResult = await phoneAuth.signInWithCredential(credential);
    } catch (error) {
      phoneAuthStatus = (PhoneAuthStatus.HasError);
      notifyListeners();
      return;
    }
    currentUser = authResult.user;
    phoneAuthStatus = (PhoneAuthStatus.SignedIn);
    notifyListeners();
    return;
  }

  Future<void> signOut() async {
    phoneAuth.signOut();
    phoneAuthStatus = (PhoneAuthStatus.SignedOut);
    notifyListeners();
  }
}
init()函数应返回PhoneAuthStatus.SignedIn。但是,它返回PhoneAuthStatus.SignedOut,因为当前用户为空。 如果我做错了什么,请帮帮我,告诉我。多谢各位

颤振医生:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.20.0, on Microsoft Windows [Version 10.0.18363.959], locale en-US)
 
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    X Android license status unknown.
      Try re-installing or updating your Android SDK Manager.
      See https://developer.android.com/studio/#downloads or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions.
[√] Android Studio (version 4.0)
[√] VS Code (version 1.47.3)
[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

是否将
notifyListeners()
置于if块之外进行检查

     currentUser = await phoneAuth.currentUser();
    if (currentUser == null) {
      phoneAuthStatus = PhoneAuthStatus.SignedOut;
     
    } else {
      phoneAuthStatus = PhoneAuthStatus.SignedIn;
    }
 notifyListeners();
另一种猜测是,您正在init()方法中调用future方法。也许这就是为什么您总是将身份验证状态作为注销

“通过设计,init方法是同步的。您不必在init方法中创建FutureBuilder小部件,而可以在重写的生成方法中返回FutureBuilder小部件。请在此处随意查看FutureBuilder小部件的文档,以获取如何工作的示例。”


因此,我认为最好使用未来的构建器来验证用户的存在。

是否将
notifyListeners()
放置在if块之外

     currentUser = await phoneAuth.currentUser();
    if (currentUser == null) {
      phoneAuthStatus = PhoneAuthStatus.SignedOut;
     
    } else {
      phoneAuthStatus = PhoneAuthStatus.SignedIn;
    }
 notifyListeners();
另一种猜测是,您正在init()方法中调用future方法。也许这就是为什么您总是将身份验证状态作为注销

“通过设计,init方法是同步的。您不必在init方法中创建FutureBuilder小部件,而可以在重写的生成方法中返回FutureBuilder小部件。请在此处随意查看FutureBuilder小部件的文档,以获取如何工作的示例。”

因此,我认为最好使用future builder来验证用户的存在。

我已经正确使用了notifyListeners()。我会在FutureBuilder中尝试并让您知道。谢谢。我已经正确地使用了notifyListeners()。我会在FutureBuilder中尝试并让您知道。谢谢