news 2026/5/20 8:14:27

用 Drift 实现 Repository 无缝接入本地缓存/数据库(SWR:先快后准)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
用 Drift 实现 Repository 无缝接入本地缓存/数据库(SWR:先快后准)

1)依赖与初始化(pubspec 思路)

常见组合(按你项目选):

  • drift

  • drift_flutter(Flutter 项目推荐)

  • sqlite3_flutter_libs(iOS/Android 自带 sqlite)

  • path_provider+path

(版本你用最新即可)

2)Drift 表结构:profiles

关键字段:updatedAtMs用来做 TTL / 过期判断

import 'package:drift/drift.dart'; class Profiles extends Table { TextColumn get id => text()(); // 主键 TextColumn get name => text()(); TextColumn get avatar => text().nullable()(); IntColumn get updatedAtMs => integer()(); // 记录更新时间(毫秒) @override Set<Column> get primaryKey => {id}; }

3)Database 定义(AppDatabase)

使用drift_flutterNativeDatabase.createInBackground最省心。

import 'dart:io'; import 'package:drift/drift.dart'; import 'package:drift/drift.dart' as drift; import 'package:drift_flutter/drift_flutter.dart'; part 'app_database.g.dart'; @DriftDatabase(tables: [Profiles], daos: [ProfileDao]) class AppDatabase extends _$AppDatabase { AppDatabase() : super(_openConnection()); @override int get schemaVersion => 1; } LazyDatabase _openConnection() { return LazyDatabase(() async { return drift_flutter.openDatabase( name: 'app.db', native: const DriftNativeOptions( shareAcrossIsolates: true, ), ); }); }

说明:

  • part 'app_database.g.dart';需要 build_runner 生成

  • 文件名你可以按你工程改,比如db.dart

4)DAO:ProfileDao(watch + get + upsert)

Repository 最喜欢 DAO 提供这几个方法。

import 'package:drift/drift.dart'; import 'app_database.dart'; part 'profile_dao.g.dart'; @DriftAccessor(tables: [Profiles]) class ProfileDao extends DatabaseAccessor<AppDatabase> with _$ProfileDaoMixin { ProfileDao(AppDatabase db) : super(db); Stream<Profile?> watchProfile(String id) { return (select(profiles)..where((t) => t.id.equals(id))) .watchSingleOrNull(); } Future<Profile?> getProfile(String id) { return (select(profiles)..where((t) => t.id.equals(id))) .getSingleOrNull(); } Future<void> upsertProfile(ProfilesCompanion data) async { await into(profiles).insertOnConflictUpdate(data); } Future<void> deleteProfile(String id) async { await (delete(profiles)..where((t) => t.id.equals(id))).go(); } Future<void> clearAll() async { await delete(profiles).go(); } }

5)Domain Model + Mapper(别省略,后期维护靠它)

Domain Model

class ProfileModel { final String id; final String name; final String? avatar; ProfileModel({required this.id, required this.name, this.avatar}); }

Mapper:Drift Row ↔ Domain

Drift 的 row 类型叫Profile(与表名 Profiles 对应),下面示例:

import 'app_database.dart'; class ProfileMapper { static ProfileModel toModel(Profile row) { return ProfileModel( id: row.id, name: row.name, avatar: row.avatar, ); } static ProfilesCompanion toCompanion(ProfileModel m) { return ProfilesCompanion.insert( id: m.id, name: m.name, avatar: Value(m.avatar), updatedAtMs: DateTime.now().millisecondsSinceEpoch, ); } }

6)Remote API(Dio 获取网络数据)

接口层只负责“拿远端”,Repository 负责策略。

abstract class ProfileApi { Future<ProfileModel> fetchProfile(String id); }

7)Repository:DB 单一事实源 + refresh 回写(推荐)

7.1 watch:页面自动更新

class ProfileRepository { final ProfileApi api; final ProfileDao dao; ProfileRepository({required this.api, required this.dao}); Stream<ProfileModel?> watchProfile(String id) { return dao.watchProfile(id).map((row) => row == null ? null : ProfileMapper.toModel(row)); } Future<void> refreshProfile(String id) async { final remote = await api.fetchProfile(id); await dao.upsertProfile(ProfileMapper.toCompanion(remote)); } }

页面使用方式(思路):

  • UI 订阅watchProfile(id)→ 立即显示 DB 数据

  • 下拉刷新调用refreshProfile(id)→ 网络成功后写 DB → UI 自动更新

8)再加一层“TTL 过期策略”(先快后准 + 后台刷新)

如果你还想:DB 有旧数据先出,再判断过期自动刷新:

class CachePolicy { final Duration ttl; CachePolicy(this.ttl); bool isExpired(int updatedAtMs) { final age = DateTime.now().millisecondsSinceEpoch - updatedAtMs; return age > ttl.inMilliseconds; } } class ProfileRepositoryWithTtl { final ProfileApi api; final ProfileDao dao; final CachePolicy policy; ProfileRepositoryWithTtl({required this.api, required this.dao, required this.policy}); Stream<ProfileModel?> watchProfile(String id) { return dao.watchProfile(id).map((row) => row == null ? null : ProfileMapper.toModel(row)); } /// 页面进入时调用一次:如果过期就后台刷新 Future<void> refreshIfExpired(String id) async { final cached = await dao.getProfile(id); if (cached == null || policy.isExpired(cached.updatedAtMs)) { await refreshProfile(id); } } Future<void> refreshProfile(String id) async { final remote = await api.fetchProfile(id); await dao.upsertProfile(ProfileMapper.toCompanion(remote)); } }

9)和 401 自动刷新 Token 如何衔接?

完全无感:
Repository 调api.fetchProfile,Dio 层的 RefreshInterceptor 处理 401。
refresh 失败就触发全局onAuthExpired,UI 统一跳登录,Repository 不管。

10)你需要生成代码(Drift 必做)

你有part '*.g.dart'的文件,需要 build:

flutter pub run build_runner build --delete-conflicting-outputs
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/15 4:42:55

基于SpringBoot的冷链运输生鲜销售系统计算机毕业设计项目源码文档

项目整体介绍在生鲜电商规模化、冷链管控精细化需求升级的背景下&#xff0c;传统生鲜销售存在 “冷链轨迹不可溯、损耗率高、订单履约低效” 的痛点&#xff0c;基于 SpringBoot 构建的冷链运输生鲜销售系统&#xff0c;适配消费者、冷链运维人员、商家、平台管理员等角色&…

作者头像 李华
网站建设 2026/5/15 4:42:34

一次架构调整,让人工介入减少了一半

如果你维护过一段时间的采集系统&#xff0c;大概率会经历这样一个阶段&#xff1a; 一开始一切都很顺利&#xff0c;requests 一跑&#xff0c;数据就回来了。 后来目标站点开始限速&#xff0c;你加了代理。 再后来&#xff0c;403、429、超时轮番出现&#xff0c;报警开始刷…

作者头像 李华
网站建设 2026/4/24 7:46:39

windows比较好用的翻译软件

一般大多人会用网易有道翻译&#xff0c;或者欧陆词典 但是这两个有点太笨重&#xff0c;尤其是现在的有道翻译打开就需要好几秒。 所以推荐两个比较轻快的&#xff0c;可以集合多家api使用 1.STranslate https://github.com/STranslate/STranslate 2.pot-desktop https://g…

作者头像 李华
网站建设 2026/5/17 4:25:35

【AI革命】Deep Research深度研究:大模型如何实现复杂任务推理?零基础也能学会的多智能体技术!

前言 近期&#xff0c;Deep Research 能力逐渐成为大型 AI 公司和开源社区的研究重点。与传统的知识问答不同&#xff0c;Deep Research 强调模型在多步骤任务中稳定检索、推理与信息整合的能力&#xff0c;这种能力是模型在复杂研究场景中生成可靠结论等基石。OpenAI 的 ChatG…

作者头像 李华
网站建设 2026/5/3 10:02:18

文档识别接口:赋能企业高效办公与加速信息的数字化转型

在数字经济加速发展的今天&#xff0c;文档作为信息承载的核心载体&#xff0c;其处理效率直接关系到企业的运营效能和智能化水平。据不完全统计&#xff0c;全球80%以上的业务数据仍以非结构化形式存在&#xff0c;其中纸质或扫描文档占据相当比重。传统依赖人工录入与校对的文…

作者头像 李华