此代码来自我的本地存储数据文件,根据我的测试,我猜该错误位于 setUser 方法或下一个文件的某个位置。
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();
}
}
如果从从云 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();
}
}
任何帮助,将不胜感激。