我是新手,并尝试将值保存在sharepreferences中,在其中我为如下所示的sharepreferences创建了另一个类。
首先,您可以看到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
请让我知道问题是在检索时是否不转换首选项还是问题所在。
答案 0 :(得分:0)
可能是因为您要从 import osimage_path
from shutil import copyfile
images_folder = '/root/facedetect'
images = os.listdir(images_folder)
detected_faces_folder = '/root/detected_faces'
for image in images:
image_path = os.path.join(images_folder, image)
img = cv2.imread(image_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
...
...
if detected_faces:
copyfile(image_path, os.path.join(detected_faces_folder, image))
和.getBool
获取的值也需要.getString
编辑。