简体   繁体   中英

Flutterfire GetX AuthFlow

I am using flutter, firebase auth, and getx to build an app. After checking if the user exists, the page should redirect normally. But it is not. What am I doing wrong? I have been stuck on this for some time now but I cannot figure it out. It is stuck on the loading screen especially when the user is null. If I create a user in firebase, it redirects normally.

class AuthController extends GetxController {
  static AuthController authInstance = Get.find();
  final phone = ''.obs;
  final phoneOtp = ''.obs;
  final verificationID = ''.obs;
  final FirebaseAuth auth = FirebaseAuth.instance;
  late Rx<User?> firebaseUser;

  @override
  void onReady() {
    super.onReady();
    firebaseUser = Rx<User?>(auth.currentUser);
    firebaseUser.bindStream(auth.userChanges());

    ever(firebaseUser, _setInitialScreen);
  }

  _setInitialScreen(User? user) {
    if (user == null) {
      Get.offAllNamed("/onboarding");
    } else {
      Get.offAllNamed("/landing");
    }
  }
}

Firebase has a function to check the user's status, use it instead it's easier and more efficient. Use a streambuilder

StreamBuilder(
        stream: AuthServices().onChangedUser,
        builder: (context, snapshot) {
          return snapshot.data == null ? Get.offAllNamed("/onboarding"); : Get.offAllNamed("/landing");;
        },
      ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM