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;
}
}
CodePudding user response:
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));
}
CodePudding user response:
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
};
}