Flutter数据持久化

前言

欢迎阅读Flutter系列教程,本文讲解Flutter的数据持久化。主要分三块内容:
1.使用Sqlite
2.读写文件
3.存储键值对

Sqlite的使用

如果你的APP需要经常在本地存储查询大量数据,就选择数据库。通常使用数据库来进行数据的增删改查比其他数据持久化方案速度更快。Flutter里面可以通过sqflite插件来操作Sqlite

sqlite简介

如果你用过数据库,可以略过此部分,使用时留意一下代码语法就行。

实战

  1. 首先要添加依赖,在pubspec.yaml文件里添加,添加完以后下载一下。
dependencies:
  flutter:
    sdk: flutter
  sqflite:
  path:
  1. 引入库
import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
  1. 创建数据库模型
    在创建数据库之前,先花几分钟对数据库要存储的信息建立一个对象。
class Device{
  final String mac;
  final String name;
  final int ownerId;

  Device({
    required this.mac,
    required this.name,
    required this.ownerId,
  });
  Map<String, dynamic> toMap() {
  return {
    'mac': mac,
    'name': name,
    'ownerId': ownerId,
  };
}
}
  1. 写一下建表语句
    如下,当调用openDatabase就新建了一个数据库链接。

当调用openDatabase时,如果数据库不存在的话就会执行onCreate回调函数,所以我们可以把建表语句放在onCreate函数里。

  final database = openDatabase(
    join(await getDatabasesPath(), 'fridge_database.db'),
    onCreate: (db, version) {
      return db.execute(
        'CREATE TABLE devices(mac TEXT PRIMARY KEY, name TEXT, ownerId INTEGER)',
      );
    },
    version: 1,
  );
  1. 插入
  Future<void> insertDevice(Device device) async {
    final db = await database;
    await db.insert(
      'devices',
      device.toMap(),
      conflictAlgorithm: ConflictAlgorithm.replace, //指定一种策略,当有相同的设备时,直接替换掉。
    );
  }
  1. 查询
  Future<List<Device>> devices() async {
    final db = await database;
    final List<Map<String, dynamic>> maps = await db.query('devices');//查询devices表,此时返回的时List类型,元素为Map类型
    return List.generate(maps.length, (i) { //转换成Device类型
      return Device(
        mac: maps[i]['mac'],
        name: maps[i]['name'],
        ownerId: maps[i]['ownerId'],
      );
    });
  }

  1. 如下代码,根据mac修改设备信息
  Future<void> updateDevice(Device device) async {
    final db = await database;
    await db.update(
      'devices',
      device.toMap(),
      where: 'mac = ?',
      whereArgs: [device.mac],
    );
  }

  1. 根据mac删除设备信息
  Future<void> deleteDevice(String mac) async {
    final db = await database;
    await db.delete(
      'devices',
      where: 'mac = ?',
      whereArgs: [mac],
    );
  }

文件读写

添加依赖:

 path_provider:

文件系统分为应用目录和临时目录,在安卓里分别对应AppDatagetCacheDir(),ios对应NSDocumentDirectoryNSCachesDirectory

临时目录随时可能被系统删除,应用目录只有在系统写在app时才会删除。

下面的代码获取app目录

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();
  return directory.path;
}

然后根据app可以拼接成完整的文件目录,根据目录返回File对象

Future<File> get _localFile async {
  final path = await _localPath;
  return File('$path/counter.txt');
}

此时就可以使用这个File对象进行文件读写了

Future<File> writeCounter(int counter) async {

  final file = await _localFile;
  
  return file.writeAsString('$counter');
}

Future<int> readCounter() async {
  try {
    final file = await _localFile;
    
    final contents = await file.readAsString();
    
    return int.parse(contents);
    
  } catch (e) {
    return 0;
  }
}

此处读写的文件需要root才能查看哦!

键值对

依然添加依赖

 shared_preferences:

对应安卓里面的SharedPreferencesiso的NSUserDefaults ,用来存储少量的键值对。

写:

final prefs = await SharedPreferences.getInstance(); //获取引用
prefs.setInt('counter', counter);//写值
final counter = prefs.getInt('counter') ?? 0;//读值
prefs.remove('counter');//删除

注意:

  • 仅支持int, double, bool, string, and stringList.
  • 不要存大量数据
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值