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?