当前位置:   article > 正文

Flutter本地数据存储_flutter本地缓存数据

flutter本地缓存数据

总的来说目前有三种手段

sqflite:https://pub.dartlang.org/packages/sqflite
shared_preferences:https://pub.dartlang.org/packages/shared_preferences
path_provider:https://pub.dartlang.org/packages/path_provider

sqflite

sqflite是一款轻量级的关系型数据库,类似SQLite。
Flutter平台我们使用sqflite库来同时支持Android 和iOS。
使用介绍
1.首选需要在pubspec.yaml 导入库
在这里插入图片描述
2.dart类中导入库

在这里插入图片描述
image.png
3.创建-增删改查

创建:
image.png
image.png
增:
image.png
删:
image.png
改:
image.png
查:
在这里插入图片描述
shared_preferences
1.首选需要在pubspec.yaml 导入库
shared_preferences 0.5.3+4
2在dart中引入库

在这里插入图片描述
目前支持这五种类型,类似安卓中的用法不再赘述
有的人可能会问,那json对象怎么保存呢?比如User
LocalStorage.save(Constants.USER_INFO, response.toString());
直接把json.toString,转化成String保存即可
用的时候

 static User getUser() {
    ///获取本地登录用户信息
    var userText = LocalStorage.get(Constants.USER_INFO);
    if (userText != null) {
      var userMap = json.decode(userText);
      User user = User.fromJson(userMap);
      return user;
    } else {
      return User.empty();
    }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

path_provider

在某些时候可能需要下载或保存文件到手机本地,这时候使用文件读写接口可以实现。

在 Flutter 里实现文件读写,需要使用 path_provider 和 dart 的 io 模块。path_provider 负责查找 iOS/Android 的目录文件,IO 模块负责对文件进行读写。

首先安装依赖:

dependencies:
path_provider 1.3.0
通常来说会有以下几个步骤:

  • 找到正确的本地路径。
  • 创建对文件位置的引用。
  • 将数据写入文件。
  • 从文件中读取数据。

本地路径
path_provider 插件提供了一种平台不可知的方式来访问设备文件系统上的常用位置。 该插件当前支持访问两个系统文件位置:

  • 临时目录: 一个临时目录(缓存),系统可以随时清除。 在 iOS 上,这对应于 NSTemporaryDirectory() 返回的值。在 Android 上,这是 getCacheDir() 回的值。
  • 文档目录:应用程序的目录,用于存储只有它可以访问的文件。
    只有当应用程序被删除时,系统才会清除目录。 在 iOS 上,这对应于 NSDocumentDirectory。 在 Android 上这是
    AppData 目录。

接下来使用 path_provider 查找本地路径。

import 'package:path_provider/path_provider.dart';

localPath() async {
    try {
        var tempDir = await getTemporaryDirectory();
        String tempPath = tempDir.path;

        var appDocDir = await getApplicationDocumentsDirectory();
        String appDocPath = appDocDir.path;

        print('临时目录: ' + tempPath);
        print('文档目录: ' + appDocPath);
    }
    catch(err) {
        print(err);
    }
}

// 临时目录: /data/user/0/com.example.myapp/cache
// 文档目录: /data/user/0/com.example.myapp/app_flutter
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

文件位置引用
一旦我们知道在哪里存储文件,我们需要创建一个文件的完整位置的引用。 我们可以使用 dart:io 库中的 File 类来实现此目的。

localFile(path) async {
    return new File('$path/counter.json');
}
  • 1
  • 2
  • 3

读写文件
在对文件引用之后,就可以对文件进行读写操作了。

// 读取 json 数据
readJSON() async {
    try {
        final file = await localFile(await localPath());
        String str = await file.readAsString();
        return json.decode(str);
    }
    catch (err) {
        print(err);
    }
}

// 写入 json 数据
writeJSON(obj) {
    try {
        final file = await localFile(await localPath());
        return file.writeAsString(encode(obj));
    }
    catch (err) {
        print(err);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/308743
推荐阅读
相关标签
  

闽ICP备14008679号