Features

Creates an extended model that is serializable based on your models.

The libraries serializable_models_rest and serializable_models_sqlite may exist to go with this.

Getting started

Add the serializable_models to your pubspec.yaml and build_runner to your dev dependencies.

Upon changes run:

flutter pub run build_runner build

Usage

  1. Your first constructor is used when needed in generated code. Make sure you create one.
  2. Your classes must be private, have a part declared, be abstract and extend SerializableModelBase
  3. If you need to change field serialization you have to change the library directly. This may change in the future

Below is the simplest example. To see more, check example/lib/models - showing models that don't have generated code, and all the options you have when generating

import 'package:serializable_models/annotations.dart';
import 'package:serializable_models/model_base.dart';

part 'item.o.dart';

@SerializableModel()
abstract class _Item extends SerializableModelBase {
  @Pk()
  int id;
  String name;

  _Item({
    required this.id,
    required this.name,
  });
}

After running build, this results in this: lib/models/task.o.dart

// Source library: package:example/models/item.dart

part of 'item.dart';

class Item extends _Item {
  @override
  get pk => id;

  Item({
    required int id,
    required String name,
  }) : super(
    id: id,
    name: name,
  );

  factory Item.fromSerializable(Map<String, dynamic> from) {
    var instance = Item(
      id: from["id"],
      name: from["name"],
    );

    return instance;
  }

  @override
  Item clone() {
    return Item(
      id: id,
      name: name,
    );
  }

  @override
  void loadSerializable(Map<String, dynamic> from) {
    id = from["id"];
    name = from["name"];
  }

  @override
  Map<String, dynamic> toSerializable() {
    return {
      "id": id,
      "name": name,
    };
  }
}

Libraries

serializable_models.annotations
serializable_models.builder
serializable_models.model_base