flutter-isar
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseIsar Database
Isar 数据库
- Use Isar as the primary local database for structured data persistence
- Define collections using annotation with
@collectionfieldId - Place collection definitions in the directory of the relevant feature
data/local/ - Run after modifying collections
dart run build_runner build --delete-conflicting-outputs
- 使用 Isar 作为结构化数据持久化的首选本地数据库
- 使用带有字段的
Id注解定义集合@collection - 将集合定义放在对应功能模块的目录下
data/local/ - 修改集合后执行
dart run build_runner build --delete-conflicting-outputs
Schema Design
Schema 设计
- Keep collections focused — one collection per domain entity
- Use annotations for fields queried frequently
@Index - Use for enum fields
@enumerated - Use and
Linksfor relationships between collectionsBacklinks - NEVER store derived/computed values — compute them in the domain layer
- 保持集合职责单一——每个领域实体对应一个集合
- 对高频查询的字段使用注解
@Index - 枚举字段使用注解
@enumerated - 使用和
Links处理集合之间的关系Backlinks - 绝对不要存储派生/计算值——在领域层完成相关计算
Migrations
数据迁移
- Isar handles additive schema changes automatically (new fields, new collections)
- For breaking changes (renamed fields, removed fields), write migration logic in the repository
- Version your database schema and check version on app startup
- Isar会自动处理增量式Schema变更(新增字段、新增集合)
- 遇到破坏性变更(字段重命名、字段删除)时,需在仓储层编写迁移逻辑
- 给数据库Schema添加版本控制,在应用启动时校验版本
Repository Pattern for Local Data
本地数据的仓储模式
- Local DataSource wraps all Isar ,
put,get,deletecallswatch - Repositories orchestrate between remote and local DataSources
- Repositories decide cache-first vs network-first strategy per use case
- 本地数据源(Local DataSource)封装所有Isar的、
put、get、delete调用watch - 仓储层负责协调远程和本地数据源
- 仓储层可根据不同使用场景决定采用缓存优先还是网络优先策略
Offline-First Patterns
离线优先模式
- Cache-First: Read from Isar first, fetch from network in background, update Isar on success
- Network-First: Fetch from network, fall back to Isar on failure
- Write-Behind: Write to Isar immediately, sync to server asynchronously (queue pending changes)
- Always show cached data immediately while refreshing in background
- 缓存优先:优先从Isar读取数据,后台发起网络请求,请求成功后更新Isar数据
- 网络优先:优先发起网络请求,请求失败时回退到Isar读取本地数据
- 写延后:立即写入Isar,异步同步到服务端(将待变更内容加入队列)
- 后台刷新数据时,始终优先立即展示已缓存的数据
Reactive Queries
响应式查询
- Use Isar's or
watchLazy()to stream changes to the UI via BLoCwatch() - Wrap Isar watch streams in the repository and expose as
Stream<List<DomainModel>> - Dispose stream subscriptions properly in BLoC method
close()
- 使用Isar的或
watchLazy()方法,通过BLoC向UI层发送变更流watch() - 在仓储层封装Isar监听流,对外暴露为类型
Stream<List<DomainModel>> - 在BLoC的方法中正确释放流订阅资源
close()
Secure Storage
安全存储
- Use for sensitive key-value data (tokens, encryption keys)
flutter_secure_storage - Use ONLY for non-sensitive user preferences (theme, locale, onboarding flags)
SharedPreferences - NEVER store passwords, tokens, or secrets in or plain Isar
SharedPreferences
- 敏感键值对数据(令牌、加密密钥)使用存储
flutter_secure_storage - 仅非敏感用户偏好设置(主题、语言、新手引导标记)可以使用存储
SharedPreferences - 绝对不要在或未加密的Isar中存储密码、令牌或密钥类信息
SharedPreferences