Home > Software engineering >  Flutter, How to use multiple shared preferences
Flutter, How to use multiple shared preferences

Time:06-04

My goal was to avoid users getting logged out once they close and reopened the app. So, I used shared_preferences to store the user's email and password locally so, that whenever the user reopens the app I could use firebase's signInWithEmailAndPassword and provide locally stored email and password to it. I used setString() to store email password but I was able to store only email and not the password, I get the error "Password is null" How do I solve this. Did I do the code right?

Storing email and password when user registers in app

onPressed: () async {
                  try {
                    final user =
                        await _auth.createUserWithEmailAndPassword(
                            email: email, password: password);
                    if (user != null) {
                      final SharedPreferences sharedPreferences =
                          await SharedPreferences.getInstance();
                      sharedPreferences.setString(
                        'email',
                        email,
                      );
                      sharedPreferences.setString('password', password);
                      Get.to(() => const BorrowerList());
                    }
                  } catch (e) {
                    Get.snackbar('Error', e.toString(),
                        backgroundColor: (Colors.red));
                  }
                },

fetching email password and login after reopening.

  @override
  void initState() {
    super.initState();
    validateUserAuth();
  }

  final _firestore = FirebaseFirestore.instance;
  void validateUserAuth() async {
    final SharedPreferences sharedPreferences =
        await SharedPreferences.getInstance();
    var obtainedEmail = sharedPreferences.getString('email');
    var obtainedPassword = sharedPreferences.getString('password');
    if (obtainedEmail != null || obtainedPassword != null) {
      setState(() {
        loggedinUserEmail = obtainedEmail!;
        loggedinUserPassword = obtainedPassword!;
      });
      if (loggedinUserEmail != null || loggedinUserPassword != null) {
        final loggedinuser = await auth.signInWithEmailAndPassword(
            email: loggedinUserEmail, password: obtainedPassword!);
        if (loggedinuser != null) {
          Get.to(() => const BorrowerList());
        }
      }
    } else {
      Get.to(() => AuthScreen());
    }
  }

CodePudding user response:

The error is apparent. You can't save a null value in shared_preferences.

Your code is not complete. somehow your password variable is null.

You can paste complete code so we can help you better

CodePudding user response:

Can you provide more information about the project? So far, it's obvious that you're trying to store a null value in shared_preferences. You can't do that. I'm pretty sure the problem is that when the user enters a password, it's not stored in the password variable here:

                  try {
                    final user =
                        await _auth.createUserWithEmailAndPassword(
                            email: email, password: password);
                    if (user != null) {
                      final SharedPreferences sharedPreferences =
                          await SharedPreferences.getInstance();
                      sharedPreferences.setString(
                        'email',
                        email,
                      );
                      // NULLABLE VARIABLE!
                      sharedPreferences.setString('password', password);
                      Get.to(() => const BorrowerList());
                    }
                  } catch (e) {
                    Get.snackbar('Error', e.toString(),
                        backgroundColor: (Colors.red));
                  }
                },

Additionally, it is not recommended to use shared_preferences for storing sensitive data. Flutter Secure Storage is much better suited for this.

  • Related