0

I am working on my flutter project where I want to use sharedpreferences. Look at the code below:

Future<String?> getCredentials() async {
   final localStorage = await SharedPreferences.getInstance();
   final email = localStorage.getString('email');
   final password = localStorage.getString('password');
   return email, password;
}

This is my getCredentials funtion I want this function to return email and password as different parameters but dart doesn't allow me can you please help me How can I do it?

Whole SharedPreference Code:

import 'package:shared_preferences/shared_preferences.dart';

class sharedPreference {
  Future<String?> saveCredentials({
    required String email,
    required String password,
  }) async {
    final localStorage = await SharedPreferences.getInstance();
    await localStorage.setString('email', email);
    await localStorage.setString('password', password);
  }

  Future<String?> getCredentials() async {
    final localStorage = await SharedPreferences.getInstance();
    final email = localStorage.getString('email');
    final password = localStorage.getString('password');
    return email, password;
  }
}
3
  • It's a bad idea to store a password. Commented Nov 16, 2022 at 10:51
  • @Patrick Is there any other way If i can't store passsword but still user can login without credentials if they logged in once? Commented Nov 16, 2022 at 12:45
  • Store only a password hash, for example with the crypto package. Commented Nov 16, 2022 at 13:10

2 Answers 2

3

Just create class. You can even add methods to Credentials later. Like secure compare to compare passwordHash with constant time.

class Credentials {
  Credentials(this.email, this.passwordHash);

  final String email;
  final String passwordHash;
}

Future<Credentials> getCredentials() async {
   final localStorage = await SharedPreferences.getInstance();
   final email = localStorage.getString('email');
   final passwordHash = localStorage.getString('passwordHash');
   return Credentials(email, passwordHash));
}

Edit use crypto to get hash of password:

import 'dart:convert';
import 'package:crypto/crypto.dart';

String getHash(String plainPassword) {
    return sha256.convert(utf8.encode(plainPassword)).toString();
}
Sign up to request clarification or add additional context in comments.

Comments

0

change return type String to Map<String,dynamic>

Future<Map<String,dynamic>> getCredentials() async {
   final localStorage = await SharedPreferences.getInstance();
   final email = localStorage.getString('email');
   final password = localStorage.getString('password');
   return {
'email':email,
'password':password

};
}

4 Comments

and there is a advise not to store password...but if u want..u may set return type to Map<String,dynamic>
tbh, you should create class with fields email and password and return it for better readability (and intellisense).
@IrfanGanatra Is there any other way If i can't store passsword but still user can login without credentials if they logged in once?
@MrFin you only need to store hash of password. Use crypto to hash it in store in database or whatever. And then user tries to logging you can hash his input and compare it to stored value. This will ensure that even if database get leaked you won't expose passwords in plain text. Also plz for the god sake use my answer. Ngl if someone will ever see this in code they might die from cringe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.