Search code examples
fluttermobilefirebase-authenticationflutter-getx

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");
    }
  }
}


Solution

  • 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");;
            },
          ),