Flutter Data 提供了一种简化的方法来管理 Flutter 应用程序中的持久化数据模型,其灵感来自 Ember Data 和 ActiveRecord。它通过为本地和远程数据源提供统一的 API,简化了 CRUD 操作、状态管理和跨设备的数据同步。该包与 Flutter 生态系统无缝集成,支持类型安全的模型、自动序列化和内置缓存。开发者可以轻松定义数据模型和关系,从而在无需样板代码的情况下实现高效的数据处理。
适合场景
管理离线优先的数据工作流
在本地存储和API之间同步数据
构建可扩展的 CRUD 应用程序
处理复杂的数据关系
实现响应式数据更新
主要特点
自动模型序列化
内置的 SQLite 持久化存储
响应式状态管理
对 REST 和 GraphQL API 的支持
类型安全的数据模型和查询
适合对象
需要离线数据访问的应用程序
使用 REST 或 GraphQL 后端的项目
寻求减少样板代码的开发者
采用类似MVC数据架构的团队
具有复杂数据关系的应用程序
使用建议
新用户的上手难度
与成熟的替代方案相比,文档有限
可能需要为自定义端点进行额外设置
对于数据需求极少的简单应用来说并不理想
依赖外部依赖项,例如 json_serializable
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Flutter Data
Persistent reactive models in Flutter with zero boilerplate
Flutter Data is a local-first data framework with a customizable REST client and powerful model relationships, built on Riverpod.
In Flutter Data, every model gets its default adapter. These adapters can be extended by mixing in custom adapters.
Annotate a model with @DataAdapter and pass a custom adapter:
@JsonSerializable()
@DataAdapter([MyJSONServerAdapter])
classUserextendsDataModel<User> {
@overridefinalint? id; // ID can be of any typefinalString name;
User({this.id, requiredthis.name});
// `User.fromJson` and `toJson` optional
}
mixinMyJSONServerAdapteronRemoteAdapter<User> {
@overrideStringget baseUrl => "https://my-json-server.typicode.com/flutterdata/demo/";
}
After code-gen, Flutter Data will generate the resulting Adapter<User> which is accessible via Riverpod's ref.users or container.users.
@overrideWidgetbuild(BuildContext context, WidgetRef ref) {
final state = ref.users.watchOne(1);
if (state.isLoading) {
returnCenter(child: constCircularProgressIndicator());
}
final user = state.model;
returnText(user.name);
}
ref.users.watchOne(1) will make a background HTTP request (to https://my-json-server.typicode.com/flutterdata/demo/users/1 in this case), deserialize data and listen for any further local changes to the user.
state is of type DataState which has loading, error and data substates.
In addition to the reactivity, models have ActiveRecord-style extension methods so the above becomes:
Appwrite Offline. A Flutter Data adapter for Appwrite that provides offline support, real-time updates, and seamless integration with Flutter Data's powerful features.
🚨 BREAKING CHANGES IN 2.0
All methods are now directly on Adapter, there is no Repository, RemoteAdapter or LocalAdapter. Any method you are looking for is probably on Adapter, for example, findAll from LocalAdapter is now called findAllLocal
For initialization we no longer call the configure... method on the Riverpod overrides, we just do localStorageProvider.overrideWithValue and pass a LocalStorage instance; the actual initialization is done via initializeFlutterData which needs an adapter map. An adapterProvidersMap is conveniently code-generated and available on main.data.dart
Offline operations are still supported, but retries are not, it is up to clients to implement them
📚 Public API
Initialization
First you need to supply a local storage provider, via Riverpod configuration. A popular option for the base directory is using the path_provider package.