///这里我们将dynamic类型的数据转换为json串,然后进行存储
var result = json.decode(response.toString());
var userInfo = json.encode(result["obj"]); ///对象转json取的时候才能json转对象
setLocal("userInfo", userInfo);
///shared_preferences存储方法
setLocal(text, data) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(text, data);
}
///shared_preferences取出方法
Future getLocal(text) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(text);
}
///取出本地存储数据
getLocal("userInfo").then((value) => {
setState(() {
this.myinfo = json.decode(value);
})
});
flutter shared_preferences存储并取出对象简单例子
最新推荐文章于 2024-07-25 10:42:18 发布