model_json 1.0.2 copy "model_json: ^1.0.2" to clipboard
model_json: ^1.0.2 copied to clipboard

DB model managment library

model_json #

This package helps you with managing object models for databases.

Getting started #

Define some object for a database, for example User, extend it from Equatable and mixin Model.

class User extends Equatable with Model {
  final String id;
  final String name;
  final List<String> list;

  User({
    required this.id,
    required this.name,
    required this.list,
  });
}

Due to limitations in Dart, specifically an inability to use types derived from variables as parameters for generics, model_json relies on type_plus. To use model_json, add your classes to type_plus's class definitions like so:

void main() {
  . . .
  TypePlus.add<User>();
  . . .
}

Feel free to move this process to a separate function in case you have many classes.

Usage #

Parse from JSON #

Say we send a request GET /user?id= and the response body contains a user object as JSON. Let's parse it:

final client = http.Client;
final uri = Uri.http(
    "localhost:5000",
    "/user",
    queryParameters: {"id": "awd3512gf"},
);
final response = await client.get(uri);

final user = Model.fromJson<User>(response.body);

Parse to JSON #

All classes that mixin Model have toJSON method. Say we want to save a user object in the database with a request POST /user?id=&name=&list=

final client = http.Client;
final uri = Uri.http(
    "localhost:5000",
    "/user",
    queryParameters: User.toJson(),
);
final response = await client.post(uri);

Comparison of objects #

User extends Equatable, and Model implements props method. So User objects now can be compared by value.

final test1 = User(id: '1', name: 'john doe', list: ['hello', 'world']);
final test2 = User(id: '1', name: 'john doe', list: ['hello', 'world]');

test1 == test2; // true

Contributors #

  • Alexey Tkachenko (@da-the-dev)
  • Michael Zimin (@N0taName)
2
likes
110
points
2
downloads

Publisher

unverified uploader

Weekly Downloads

DB model managment library

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

equatable, type_plus

More

Packages that depend on model_json