Reputation: 1351
I have multiple controllers for my Webpage using GetX. The authController
logs the user in and calls the database api to fetch the user from firebase firestore:
await Future.delayed(Duration(seconds: 1));
btnController.value.success();
UserCredential _authResult = await _auth.signInWithEmailAndPassword(email: email.trim(), password: password);
Get.find<UserController>().user = await Database().getUser(_authResult.user!.uid);
The userController
is a very simple controller:
class UserController extends GetxController {
Rx<UserModel> _userModel = UserModel().obs;
UserModel get user => _userModel.value;
set user(UserModel value) => this._userModel.value = value;
void clear() {
_userModel.value = UserModel();
}
}
The UserModel
itself has a constructor like this, with all the fields being indicated with ?
(null-safety)
UserModel.createUser({required final data}) {
role = data["User_Role"];
firstName = data["firstName"];
lastName = data["lastName"];
age = data["age"];
privateAdress = Adress.fromLinkedMap(data: data["privateAdress"]);
dateOfBirth = data["dateOfBirth"].toDate();
driverCardNumber = data["driverCardNumber"];
driverLicenseNumber = data["driverLicenseNumber"];
email = data["email"];
id = data["employee_ID"];
lastLoginTime = data["lastLoginTime"];
mobilePhoneNumber = data["mobilePhoneNumber"];
photo = data["photo"];
}
Here is the Problem:
When I try to access the User using Get.find().user in the AuthClass
, I am able to get it. However, as soon as I try to access it anywhere else, I only get null as a result. I am also calling these methods right at the start of the webapp and all the other controllers work just fine:
Get.put(AuthController());
Get.put(UserController());
Get.put(DashboardController());
What could be the problem here?
Upvotes: 0
Views: 11773
Reputation: 1351
I figured it out! I was not able to get the UserController
data due to the binding of the FirebaseAuth
. That's because I called firebaseUser.bindStream(auth.authStateChanges())
and my Login Screen was listening to that value, hence opening the dashboard before the user was pulled:
UserCredential _authResult = await _auth.signInWithEmailAndPassword(
email: email.trim(),
password: password,
); // The dashboard was already opened here
Get.find<UserController>().user =
await Database().getUser(_authResult.user!.uid);
I simply added a bool final loggedIn = false.obs
to my auth Class and set that to true after the user has been created.
Upvotes: 1