搜索插件、代码仓与 APP 分类

插件详情v2.1.0

hydrated

一个自动持久化的 BehaviorSubject,带有 Flutter 的简单数据恢复功能。旨在与 BLoC 模式一起使用。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
385
喜欢数
29
Pub 分数
130

安装插件

flutter pub add hydrated

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Hydrated 是一个 Flutter 包,提供了一个自动持久化的 BehaviorSubject,可在应用重启后保持状态。专为与 BLoC 模式配合使用而设计,通过自动将状态保存和恢复到本地存储来简化状态管理。该包无需手动序列化或存储逻辑即可确保数据的可靠性,非常适合用于维护用户偏好、会话数据或缓存信息。支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。

适合场景

  • 保留用户偏好
  • 缓存应用状态
  • 维护会话数据
  • 重启后恢复UI状态
  • 处理离线优先场景

主要特点

  • 自动状态持久化
  • 内置序列化
  • 无缝的 BLoC 集成
  • 跨平台支持
  • 最小化样板代码

适合对象

  • 基于 BLoC 的状态管理
  • 需要离线数据保留的应用程序
  • 寻求简单持久化功能的开发者
  • 状态管理应用,设置简单
  • 需要可靠数据恢复的项目

使用建议

  • 需要对状态对象进行正确的序列化
  • 由于磁盘I/O,可能会增加初始加载时间
  • 不适合大型数据集
  • 仅限简单状态类型
  • 依赖于平台特定的存储机制

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Hydrated

Version Build License style: solid

Hydrated provides a Subject that automatically persists to Flutter's local storage and hydrates on creation!

Easy to consume

All values are persisted with shared_preferences and restored with automatic hydration.

final count$ = HydratedSubject<int>("count", seedValue: 0);

/// count$ will automagically be hydrated with 42 next time it is created
count$.add(42);

Ready for BLoC

class HydratedBloc {
  final _count$ = HydratedSubject<int>("count", seedValue: 0);

  ValueObservable<int> get count$ => _count$.stream;
  Sink<int> get setCount => _count$.sink;

  dispose() {
    _count$.close();
  }
}

Supports simple types and serialized classes

We support all shared_preferences types.

  • int
  • double
  • bool
  • String
  • List<String>
final count$ = HydratedSubject<int>("count");

We also support serialized classes with hydrate and persist arguments.

final user$ = HydratedSubject<User>(
  "user",
  hydrate: (String s) => User.fromJson(s),
  persist: (User user) => user.toJson(),
);

Reliable

Hydrated is mock tested with all supported types and is dogfooded by its creator.

Extensible

Hydrated supports any key-value data storages -- just implement the KeyValueStore interface and you will be able to use hive, flutter_secure_storage or any other persistence solution of your choice.

class MyAwesomeKeyValueStore implements KeyValueStore {
  /// your implementation here...
}

final user = HydratedSubject<User>(
  "user",
  hydrate: (String s) => User.fromJson(s),
  persist: (User user) => user.toJson(),
  keyValueStore: MyAwesomeKeyValueStore()
);

Demo

demo of Hydrated BehaviorSubject between app restarts

Original developer

hydrated was originally developed by @lukepighetti.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。