Giter VIP home page Giter VIP logo

flutter_data's Introduction

Flutter Data

tests codecov pub.dev license

Persistent reactive models in Flutter with zero boilerplate

Flutter Data is an offline-first persistence framework that gives you a configurable REST client and powerful model relationships.

Heavily inspired by Ember Data and ActiveRecord


Features

  • Repositories for all models ๐Ÿš€
  • Built for offline-first ๐Ÿ”Œ
    • Hive-based local storage at its core
    • Failure handling & retry API
  • Intuitive APIs, effortless setup ๐Ÿ’™
    • Truly configurable and composable via Dart mixins and codegen
    • Built-in Riverpod providers for all models
  • Exceptional relationship support โšก๏ธ
    • Automatically synchronized, fully traversable relationship graph
    • Reactive relationships

Check out the Documentation or the Tutorial ๐Ÿ“š where we build a TO-DO app from the ground up in record time.

Set up

See the quickstart guide for setup and boot configuration.

Prefer an example? Here's the Flutter Data sample setup app with support for Riverpod, Provider and get_it.

๐Ÿ‘ฉ๐Ÿพโ€๐Ÿ’ป Usage

For a given User model annotated with @DataRepository:

@JsonSerializable()
@DataRepository([MyJSONServerAdapter])
class User with DataModel<User> {
  @override
  final int? id; // ID can be of any type
  final String name;
  User({this.id, required this.name});
  // `User.fromJson` and `toJson` optional
}

mixin MyJSONServerAdapter on RemoteAdapter<User> {
  @override
  String get baseUrl => "https://my-json-server.typicode.com/flutterdata/demo/";
}

After a code-gen build, Flutter Data will generate a Repository<User> and utilities such as userProvider and ref.users.watchOne (Riverpod only):

@override
Widget build(BuildContext context, WidgetRef ref) {
  final state = ref.users.watchOne(1);
  if (state.isLoading) {
    return Center(child: const CircularProgressIndicator());
  }
  final user = state.model;
  return Text(user.name);
}

ref.users.watchOne(1) is a handy shortcut to the userProvider which provides ref.watch(usersRepositoryProvider).watchOneNotifier(1).

Let's see how to update the user:

TextButton(
  onPressed: () => ref.users.save(User(id: 1, name: 'Updated')),
  child: Text('Update'),
),

ref.users.watchOne(1) will make an HTTP request (to https://my-json-server.typicode.com/flutterdata/demo/users/1 in this case), parse the incoming JSON and listen for any further changes to the User โ€“ whether those are local or remote!

state is of type DataState which has loading, error and data substates.

In addition to the reactivity, DataModels get extensions and automatic relationships, ActiveRecord-style, so the above becomes:

GestureDetector(
  onTap: () =>
      User(id: 1, name: 'Updated').init(ref.read).save(),
  child: Text('Update')
),

Some other examples:

final todo = await Todo(title: 'Finish docs').init(ref.read).save();
// or its equivalent:
final todo = await ref.todos.save(Todo(title: 'Finish docs'));
// POST https://my-json-server.typicode.com/flutterdata/demo/todos/
print(todo.id); // 201

final user = await repository.findOne(1, params: { '_embed': 'todos' });
// (remember repository can be accessed via ref.users)
// GET https://my-json-server.typicode.com/flutterdata/demo/users/1?_embed=todos
print(user.todos.length); // 20

await user.todos.last.delete();

Explore the Documentation.

Fully functional app built with Flutter Data? See the code for the finished Flutter Data TO-DOs Sample App.

Compatibility

Fully compatible with the tools we know and love:

Flutter โœ… And pure Dart, too.
Flutter Web โœ… Supported!
json_serializable โœ… Fully supported (but not required)
Riverpod โœ… Supported & automatically wired up
Provider โœ… Supported with minimal extra code
get_it โœ… Supported with minimal extra code
Classic JSON REST API โœ… Built-in support!
JSON:API โœ… Supported via external adapter
Freezed โœ… Supported!

๐Ÿ“ฒ Apps using Flutter Data

logos

โž• Questions and collaborating

Please use Github to ask questions, open issues and send PRs. Thanks!

On Twitter: @flutterdata

Tests can be run with: pub run test

๐Ÿ“ License

See LICENSE.

flutter_data's People

Contributors

andreacioni avatar f3ath avatar rubgithub avatar vedadom avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.