Code Monkey home page Code Monkey logo

flutter_data's Introduction

Flutter Data

tests codecov pub.dev license

Flutter Data is the seamless way to work with persistent data models in Flutter.

Inspired by Ember Data and ActiveRecord.

Features

  • Auto-generated repositories (REST clients) for all models ๐Ÿš€
    • CRUD and custom actions on remote API
    • StateNotifier, Future and Stream APIs
  • Built for offline-first ๐Ÿ”Œ
    • uses Hive at its core for caching & local storage
    • included read/write retry offline adapter
  • Effortless setup โฐ
    • Automatically pre-configured for provider, riverpod and get_it
    • Convention over configuration powered by Dart mixins
  • Exceptional relationship support โšก๏ธ
    • Automatically synchronized, traversable relationship graph
    • Reactive relationships
  • Clean, intuitive API and minimal boilerplate ๐Ÿ’™
    • Truly configurable and composable
    • Scales very well (both up and down)

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

Getting started

See the quickstart guide in the docs.

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

For a given User model annotated with @DataRepository...

@JsonSerializable()
@DataRepository([MyJSONServerAdapter])
class User with DataModel<User> {
  @override
  final int id;
  final String name;
  User({this.id, this.name});
}

mixin MyJSONServerAdapter on RemoteAdapter<User> {
  @override
  String get baseUrl => "https://my-json-server.typicode.com/flutterdata/demo/";
}
  • id can be of int, String, etc
  • User.fromJson and toJson are not required!

After a code-gen build, Flutter Data will generate a Repository<User>:

// obtain it via Provider
final repository = context.watch<Repository<User>>();

return DataStateBuilder<List<User>>(
  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/users in this case), parse the incoming JSON and listen for any further changes to the User collection โ€“ whether those are local or remote!

state is of type DataState which has loading/error/data substates. Moreover, notifier.reload() is available, useful for the classic "pull-to-refresh" scenario.

In addition to the reactivity, a User now 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); // 201

final 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.

Compatibility

Fully compatible with the tools we know and love:

ย  ย  ย  ย  ย  ย ย ย 
Flutter ย  โœ… It can also be used with pure Dart
json_serializable ย  โœ… Not required! Other fromJson/toJson can 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
get_it ย  โœ… 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

๐Ÿ“ฒ Apps using Flutter Data

The new offline-first Scout Flutter app is being developed in record time with Flutter Data.

โž• Questions and collaborating

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

๐Ÿ“ License

See LICENSE.

flutter_data's People

Contributors

f3ath avatar rubgithub avatar

Watchers

 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.