0.4.1

DataSupport

Flutter Data models must extend DataSupport (or alternatively mix in DataSupportMixin, see below for details):

@JsonSerializable()
@DataRepository([JSONAPIAdapter, BaseAdapter])
class User extends DataSupport<User> {
  @override
  final String id;
  // ...
}

By doing this, an id getter has to be implemented. Use the type that better suits your data: int and String are the most common. In principle any object that implements toString can be used.

Most importantly, various useful methods become available on the class:

save

final user = User(id: 1, name: 'Frank');
await user.save(remote: false); // only saves locally
await user.save();

It’s syntax-sugar for Repository#save and takes the same arguments (except the model).

Important: Any Dart file that wants to use these extensions must import the library!

import 'package:flutter_data/flutter_data.dart';

VSCode PROTIP!: Type Command + . over the missing method, and choose to import!

delete

final user = repository.findOne(1);
await user.delete();

It’s syntax-sugar for Repository#delete and takes the same arguments (except the model).

find

final updatedUser = await user.find();

It’s syntax-sugar for Repository#findOne and takes the same arguments (except the ID).

watch

return DataStateBuilder<User>(
    notifier: () => user.watch(),
    builder: (context, state, notifier, _) {
      // ...
    }
);

It’s syntax-sugar for Repository#watchOne and takes the same arguments (except the ID).

This is all possible because Flutter Data automatically injected the model’s repository when implicitly calling the DataSupport constructor.

DataSupportMixin

Only for advanced use

In cases where it’s not possible to extend DataSupport, the DataSupportMixin is available.

Since mixins can’t have constructors, models won’t be able to automatically initialize and therefore initialization must be done manually.

final DataManager manager; // obtain
final user = User(name: 'Frank').init(manager);

An example of manually initializing models is in the whole Flutter Data test suite.

In case you’re wondering, Freezed can extend DataSupport. See the cookbook for more information.