kind 0.3.0 kind: ^0.3.0 copied to clipboard
An unified data layer framework that enables serialization / persistence / state management for any Dart class (without code generation!). Contains common primitives such as GeoPoint and UUID.
import 'dart:convert';
import 'package:kind/kind.dart';
void main() {
final decodedJson = {
'name': 'John Doe',
'friends': [
{'name': 'Alice'},
{'name': 'Bob'},
],
};
// Decode JSON
final person = Person.kind.jsonTreeDecode(
decodedJson,
);
print('Name:');
print(person.identifier);
// Encode JSON
final encodedJson = person.toJson();
print('JSON:');
print(json.encode(encodedJson));
}
class Person extends Entity {
static final kind = EntityKind<Person>(
name: 'Person',
packageName: 'example',
build: (b) {
b.requiredString(
id: 1,
name: 'name',
maxLengthInUtf8: 64,
examples: ['John Doe'],
field: (t) => t.name,
);
b.optionalDate(
id: 2,
name: 'dateOfBirth',
field: (t) => t.dateOfBirth,
);
b.requiredList<Person>(
id: 2,
name: 'friends',
itemsKind: Person.kind,
field: (t) => t.friends,
);
b.constructor = () => Person();
},
);
/// Name of person.
late final Field<String> name = Field<String>(this);
/// Name of person (nullable).
late final Field<Date?> dateOfBirth = Field<Date?>(this);
/// Employees of the person.
late final ListField<Person> friends = ListField<Person>(this);
@override
EntityKind getKind() => kind;
}