[英]unable to retrieve hashmap in flutter
I am new to flutter and trying to save the values in sharepreferences where I have created another class for sharepreferences like below. 我是新手,试图将值保存在sharepreferences中,我已经为下面的sharepreferences创建了另一个类。
Firstly you can see the sharedprefence class , secondly there is onsuccess method where I get values whose value I am passing to save method to save in sharedprefence class and also I am trying to retrieve the saved shareprefernce value in hasmap 首先你可以看到sharedprefence类,其次是onsuccess方法,我获取值,我传递的值保存方法以保存在sharedprefence类中,我也试图在hasmap中检索已保存的shareprefernce值
class SharedPreferencesTest {
///
/// Instantiation of the SharedPreferences library
///
final String _kNotificationsPrefs = "allowNotifications";
final String _loginsession = "loginsession";
SharedPreferences sharedPreferences;
/// ------------------------------------------------------------
/// Method that returns the user decision to allow notifications
/// ------------------------------------------------------------
Future<bool> getAllowsNotifications() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getBool(_kNotificationsPrefs) ?? false;
}
/// ----------------------------------------------------------
/// Method that saves the user decision to allow notifications
/// ----------------------------------------------------------
Future<bool> setAllowsNotifications(bool value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setBool(_kNotificationsPrefs, value);
}
/// ------------------------------------------------------------
/// Method that returns the user decision on sorting order
/// ------------------------------------------------------------
Future<String> getSortingOrder() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(_loginsession) ?? 'name';
}
/// ----------------------------------------------------------
/// Method that saves the user decision on sorting order
/// ----------------------------------------------------------
Future<bool> setSortingOrder(String value) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString(_loginsession, value);
}
Future saveSession(bool isloggedIn,String passwordval,String accessToken)async{
print("##session"+"coming");
sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setBool("is_logged_in", isloggedIn);
sharedPreferences.setString("password", passwordval);
sharedPreferences.setString("access_token", accessToken);
print("##session"+"saved");
}
Future<Map<String,String>> getLoginSession() async {
sharedPreferences = await SharedPreferences.getInstance();
print("##session"+"retroiving");
Map<String,String>sessionMap=new HashMap();
sessionMap['is_logged_in'] = sharedPreferences.getBool("is_logged_in").toString();
sessionMap['access_token'] = sharedPreferences.getString("access_token");
sessionMap['password'] = sharedPreferences.getString("password");
return sessionMap;
}
And i am trying to retrieve the saved value like the below
@override
void onLoginSuccess(LoginModel user) {
createSnackBar("successfully logged In", _scaffoldKey);
saveName(true, user.secret_key,user.access_token);
getCredential();
}
Future saveName(bool isloggedIn,String passwordval,String accessToken)async{
sharedPreferences=SharedPreferencesTest();
sharedPreferences.saveSession(true, passwordval,accessToken);
}
getCredential() async {
print("###coming inside getcredentials");
Map<String,String>sessionMap=new HashMap();
sharedPreferences=SharedPreferencesTest();
setState(() {
sessionMap= sharedPreferences.getLoginSession() as Map<String, String>;
if (sessionMap["is_logged_in"] != null) {
if (isloggedIn) {
print("###isloggedin=true");
passwordController.text = sessionMap["password"].toString();
Navigator.of(context).pushReplacementNamed('/drawer-page');
sessionMap['access_token'] = sessionMap["access_token"].toString();
sessionMap['password'] = sessionMap["password"].toString();
} else {
print("###isloggedin=false");
passwordController.clear();
}
} else {
isloggedIn = false;
}
});
return sessionMap;
}
Any help would be much appreciated. I get an error like the below
E/flutter (29347): type 'Future<Map<String, String>>' is not a subtype of type 'Map<String, String>' in typecast
Please let me know what is the problem whether the preferences are not cast while retrieving or what is the problem. 请告诉我在检索时是否未投放首选项或问题是什么问题。
这可能是因为您尝试从.getBool
和.getString
获取的值也需要await
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.