Seamlessly manage persistent data in your Flutter apps
Flutter Data is the seamless way to work with persistent data models in Flutter.
Inspired by Ember Data and ActiveRecord.
provider,
riverpodand
get_it
See the quickstart guide in the docs.
For a given
Usermodel annotated with
@DataRepository...
@JsonSerializable() @DataRepository([MyJSONServerAdapter]) class User with DataModel { @override final int id; final String name; User({this.id, this.name}); }mixin MyJSONServerAdapter on RemoteAdapter { @override String get baseUrl => "https://my-json-server.typicode.com/flutterdata/demo/"; }
idcan be of
int,
String, etc
User.fromJsonand
toJsonare not required!
After a code-gen build, Flutter Data will generate a
Repository:
// obtain it via Provider final repository = context.watch>();return DataStateBuilder>( notifier: () => repository.watchAll(); builder: (context, state, notifier, _) { if (state.isLoading) { return CircularProgressIndicator(); } // state.model is a list of 10 user items return ListView.builder( itemBuilder: (context, i) { return UserTile(state.model[i]); }, ); } }
repository.watchAll()will make an HTTP request (to
https://my-json-server.typicode.com/flutterdata/demo/usersin this case), parse the incoming JSON and listen for any further changes to the
Usercollection – whether those are local or remote!
stateis of type
DataStatewhich has loading/error/data substates. Moreover,
notifier.reload()is available, useful for the classic "pull-to-refresh" scenario.
In addition to the reactivity, a
Usernow gets extensions and automatic relationships, ActiveRecord-style:
final todo = await Todo(title: 'Finish docs').init(context).save(); // POST https://my-json-server.typicode.com/flutterdata/demo/todos/ print(todo.id); // 201final user = await repository.findOne(1, params: { '_embed': 'todos' }); // GET https://my-json-server.typicode.com/flutterdata/demo/users/1?_embed=todos print(user.todos.length); // 20
await user.todos.last.delete();
For an in-depth example check out the Tutorial.
Fully functional app built with Flutter Data? See the code for the finished Flutter Data TO-DOs Sample App.
Fully compatible with the tools we know and love:
| | | | | ----------------- | ----------------------------------------------------- | ------------------------------------------------------------- | | Flutter | ✅ | It can also be used with pure Dart | | jsonserializable | ✅ | Not required! Other
fromJson/
toJsoncan be supplied | | JSON REST API | ✅ | Great support | | JSON:API | ✅ | Great support | | Firebase | ✅ | Adapter coming soon 🎉 as well as Firebase Auth | | Provider | ✅ | Not required! Configure in a few lines of code | | Riverpod | ✅ | Not required! Configure in a few lines of code | | getit | ✅ | Not required! Configure in a few lines of code | | BLoC | ✅ | Great support | | Freezed | ✅ | Good support | | Flutter Web | ✅ | Great support | | Hive | ✅ | Flutter Data uses Hive internally for local storage | | Chopper/Retrofit | | Not required: Flutter Data generates its own REST clients |
The new offline-first Scout Flutter app is being developed in record time with Flutter Data.
Please use Github to ask questions, open issues and send PRs. Thanks!
You can also hit me up on Twitter @thefrank06
Tests can be run with:
pub run test
See LICENSE.