0.4.1

Models & Relationships FAQ

Why are save and other methods on models not available?

DataSupport extensions are syntax sugar and will only work when importing Flutter Data in the corresponding file:

import 'package:flutter_data/flutter_data.dart';

Can I use Freezed?

Yes. Actually, Flutter Data’s integration tests (and the TO-DOs example app) run off Freezed immutable models.

Here’s an example:

@freezed
@DataRepository([JSONAPIAdapter, BaseAdapter])
abstract class City extends DataSupport<City> implements _$City {
  City._();
  factory City({dynamic id, String name}) = _City;
  factory City.fromJson(Map<String, dynamic> json) => _$CityFromJson(json);
}

If you need your ID to be of a specific type such as String, make it extends IdDataSupport<String, City> instead.

IdDataSupport is not needed for classic immutable models.

Unions haven’t been tested yet.

Can I use mutable classes?

Immutable models are strongly recommended.

It is still possible to use mutable classes such as ChangeNotifiers but they haven’t really been tested.

However, id and ALL BelongsTo/HasMany relationships must be final.

How can I declare the inverse relationship?

At the moment, the inverse relationship is looked up by type and it’s not configurable. This will be fixed.

Is polymorphism supported?

Yes.

abstract class User<T extends User<T>> extends DataSupport<T> {
  final String id;
  final String name;
  User({this.id, this.name});
}

@JsonSerializable()
@DataRepository([JSONAPIAdapter, BaseAdapter])
class Customer extends User<Customer> {
  final String abc;
  Customer({String id, String name, this.abc}) : super(id: id, name: name);
}

@JsonSerializable()
@DataRepository([JSONAPIAdapter, BaseAdapter])
class Staff extends User<Staff> {
  final String xyz;
  Staff({String id, String name, this.xyz}) : super(id: id, name: name);
}

Where does Flutter Data generate code?

  • in *.g.dart files (part of your models)
  • in main.data.dart (as a library)

Can I group adapter mixins into one?

No. https://stackoverflow.com/questions/59248686/how-to-group-mixins-in-dart