博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
flutter-实现本地存储(sharePreference)
阅读量:4056 次
发布时间:2019-05-25

本文共 1313 字,大约阅读时间需要 4 分钟。

1.导包

pubspec.yaml中dependencies节点下

dependencies:  flutter:    sdk: flutter  # The following adds the Cupertino Icons font to your application.  # Use with the CupertinoIcons class for iOS style icons.  cupertino_icons: ^0.1.2  shared_preferences: ^0.5.3+4

2.封装

import 'package:shared_preferences/shared_preferences.dart';class SaveUtil {  //读取数据  static Future
getSavedByKey(String keyName) async { print('进入到读取数据'); SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); String data = await sharedPreferences.getString(keyName); print('进入到读取数据-返回数据'); return data; }//保存数据 static Future
saveBydate(String keyName, String value) async { SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); bool isOk = await sharedPreferences.setString(keyName, value); return isOk; }}

3.使用

//调用保存    Future
hadInsert= SaveUtil.saveBydate("isFirset", 'value'); hadInsert.then((onValue){ print("存储成功"); //调用读取 Future
data=SaveUtil.getSavedByKey('isFirset'); data.then((String data){ print("获取是否是第一次使用"+data); }).catchError((onError){ print("获取是否是第一次使用===异常"+onError.toString()); }); }).catchError((onError){ //存储异常 print('存储失败'+onError.toString()); });

 

转载地址:http://yxeci.baihongyu.com/

你可能感兴趣的文章
关于runat = “server”
查看>>
【opencv实战】图像素描及卡通化
查看>>
【opencv实战】哈哈镜
查看>>
【opencv学习笔记】004之Mat对象及其应用详解
查看>>
C++常用数学函数
查看>>
【积跬步以至千里】Windows无法访问指定设备,路径或文件,您可能没有合适的权限访问
查看>>
【数据结构基础笔记】第一章绪论之基本概念
查看>>
【数据结构基础笔记】第一章绪论之算法及算法分析
查看>>
【数据结构基础笔记】第二章线性表之基本概念与类型定义
查看>>
【数据结构基础笔记】第二章线性表之顺序表
查看>>
C++报错:无法打开文件“路径\Debug\文件名.exe”
查看>>
【数据结构基础笔记】第二章线性表之单链表
查看>>
【积跬步以至千里】Excel行列互换
查看>>
【YOLO学习笔记】之YOLO初体验
查看>>
【YOLO学习笔记】之YOLO配置文件详解
查看>>
【YOLO学习笔记】之YOLO v1 论文笔记1(超详细:翻译+理解)
查看>>
【YOLO学习笔记】之YOLO v1 论文笔记2(超详细:翻译+理解)
查看>>
【YOLO学习笔记——数据集】之一YOLO数据集制作1(含LabelImg工具讲解)
查看>>
【积跬步以至千里】pdf转word后数字和英文格式别扭,无法修改
查看>>
【YOLO学习笔记——数据集】之一YOLO数据集制作2
查看>>