0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I suggest stepping through the `setUser` function in the debugger to confirm that you're passing in a non null value. I also suggest always putting return types in front of your functions and using `void` if it doesn't return anything. Otherwise your return type is `dyamic` and that's one more thing that can cause unintended behavior. – Loren.A Jun 30 '21 at 22:02
  • i get that the CACHED_USER_DATA is not initialized. – Mhdi Blhoms Jul 01 '21 at 11:26
  • and i noticed that the debugger do not give any infos when stepping through the setUser method. – Mhdi Blhoms Jul 01 '21 at 11:30

0 Answers0