This code is from my local storage data file and from my testing i guess the bug is laying in the setUser method or somewhere in the next file.
class LocalStorageData extends GetxController{
static const CACHED_USER_DATA = 'CACHED_USER_DATA';
Future<UserModel?> get getUser async{
try{
UserModel userModel = await getUserData();
print(userModel);
return userModel;
}catch(e){
print(e.toString());
return null;
}
}
deleteUser()async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.clear();
}
getUserData()async{
SharedPreferences prefs = await SharedPreferences.getInstance();
var value = prefs.getString(CACHED_USER_DATA);
return UserModel.fromJson(json.decode(value!));
}
setUser(UserModel userModel)async{
SharedPreferences prefs = await SharedPreferences.getInstance();
print(userModel.name);
await prefs.setString(CACHED_USER_DATA, json.encode(userModel.ToJson()));
}
}
**this one is from my profile controller in this file the bug could be somewhere in getCurrentUser method but after testing it showed that the value is always null so maybe nothing is saved in the usermodel variable **
class ProfileViewModel extends GetxController{
@override
void onInit() {
// TODO: implement onInit
super.onInit();
getCurrentUser();
}
final LocalStorageData localStorageData = Get.find();
UserModel? _usermodel;
UserModel? get usermodel => _usermodel;
getCurrentUser()async{
await localStorageData.getUser.then((value) {
_usermodel = value;
});
}
Future<void> signOut ()async{
GoogleSignIn().signOut();
FirebaseAuth.instance.signOut();
localStorageData.deleteUser();
}
}
And this one if from a file that fetch data from cloud firestore:
class FirestoreUser{
final CollectionReference _collectionReference =
FirebaseFirestore.instance.collection('Users');
Future<void> addUserToFirestore (UserModel userModel) async{
return await _collectionReference
.doc(userModel.userId)
.set(userModel.ToJson());
}
Future<DocumentSnapshot> getCurrentUser(String uid)async{
print(uid);
return await _collectionReference.doc(uid).get();
}
}
Any help would be appreciated.