mirror of https://github.com/AMT-Cheif/drift.git
Implement fromJson factory in data classes.
This commit is contained in:
parent
e7ece27528
commit
3e1cbee3c6
|
@ -18,6 +18,12 @@ class Category {
|
||||||
description: stringType.mapFromDatabaseResponse(data['description']),
|
description: stringType.mapFromDatabaseResponse(data['description']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
factory Category.fromJson(Map<String, dynamic> json) {
|
||||||
|
return Category(
|
||||||
|
id: json['id'] as int,
|
||||||
|
description: json['description'] as String,
|
||||||
|
);
|
||||||
|
}
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'id': id,
|
'id': id,
|
||||||
|
@ -106,6 +112,14 @@ class Recipe {
|
||||||
category: intType.mapFromDatabaseResponse(data['category']),
|
category: intType.mapFromDatabaseResponse(data['category']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
factory Recipe.fromJson(Map<String, dynamic> json) {
|
||||||
|
return Recipe(
|
||||||
|
id: json['id'] as int,
|
||||||
|
title: json['title'] as String,
|
||||||
|
instructions: json['instructions'] as String,
|
||||||
|
category: json['category'] as int,
|
||||||
|
);
|
||||||
|
}
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'id': id,
|
'id': id,
|
||||||
|
@ -223,6 +237,13 @@ class Ingredient {
|
||||||
caloriesPer100g: intType.mapFromDatabaseResponse(data['calories']),
|
caloriesPer100g: intType.mapFromDatabaseResponse(data['calories']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
factory Ingredient.fromJson(Map<String, dynamic> json) {
|
||||||
|
return Ingredient(
|
||||||
|
id: json['id'] as int,
|
||||||
|
name: json['name'] as String,
|
||||||
|
caloriesPer100g: json['caloriesPer100g'] as int,
|
||||||
|
);
|
||||||
|
}
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'id': id,
|
'id': id,
|
||||||
|
@ -327,6 +348,13 @@ class IngredientInRecipe {
|
||||||
amountInGrams: intType.mapFromDatabaseResponse(data['amount']),
|
amountInGrams: intType.mapFromDatabaseResponse(data['amount']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
factory IngredientInRecipe.fromJson(Map<String, dynamic> json) {
|
||||||
|
return IngredientInRecipe(
|
||||||
|
recipe: json['recipe'] as int,
|
||||||
|
ingredient: json['ingredient'] as int,
|
||||||
|
amountInGrams: json['amountInGrams'] as int,
|
||||||
|
);
|
||||||
|
}
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'recipe': recipe,
|
'recipe': recipe,
|
||||||
|
|
|
@ -26,6 +26,15 @@ class TodoEntry {
|
||||||
category: intType.mapFromDatabaseResponse(data['category']),
|
category: intType.mapFromDatabaseResponse(data['category']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
factory TodoEntry.fromJson(Map<String, dynamic> json) {
|
||||||
|
return TodoEntry(
|
||||||
|
id: json['id'] as int,
|
||||||
|
title: json['title'] as String,
|
||||||
|
content: json['content'] as String,
|
||||||
|
targetDate: json['targetDate'] as DateTime,
|
||||||
|
category: json['category'] as int,
|
||||||
|
);
|
||||||
|
}
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'id': id,
|
'id': id,
|
||||||
|
@ -164,6 +173,12 @@ class Category {
|
||||||
description: stringType.mapFromDatabaseResponse(data['`desc`']),
|
description: stringType.mapFromDatabaseResponse(data['`desc`']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
factory Category.fromJson(Map<String, dynamic> json) {
|
||||||
|
return Category(
|
||||||
|
id: json['id'] as int,
|
||||||
|
description: json['description'] as String,
|
||||||
|
);
|
||||||
|
}
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'id': id,
|
'id': id,
|
||||||
|
@ -255,6 +270,14 @@ class User {
|
||||||
uint8ListType.mapFromDatabaseResponse(data['profile_picture']),
|
uint8ListType.mapFromDatabaseResponse(data['profile_picture']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
factory User.fromJson(Map<String, dynamic> json) {
|
||||||
|
return User(
|
||||||
|
id: json['id'] as int,
|
||||||
|
name: json['name'] as String,
|
||||||
|
isAwesome: json['isAwesome'] as bool,
|
||||||
|
profilePicture: json['profilePicture'] as Uint8List,
|
||||||
|
);
|
||||||
|
}
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'id': id,
|
'id': id,
|
||||||
|
@ -370,6 +393,12 @@ class SharedTodo {
|
||||||
user: intType.mapFromDatabaseResponse(data['user']),
|
user: intType.mapFromDatabaseResponse(data['user']),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
factory SharedTodo.fromJson(Map<String, dynamic> json) {
|
||||||
|
return SharedTodo(
|
||||||
|
todo: json['todo'] as int,
|
||||||
|
user: json['user'] as int,
|
||||||
|
);
|
||||||
|
}
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
return {
|
return {
|
||||||
'todo': todo,
|
'todo': todo,
|
||||||
|
|
|
@ -26,7 +26,8 @@ class DataClassWriter {
|
||||||
// Also write parsing factory
|
// Also write parsing factory
|
||||||
_writeMappingConstructor(buffer);
|
_writeMappingConstructor(buffer);
|
||||||
|
|
||||||
// And a serializer method
|
// And a serializer and deserializer method
|
||||||
|
_writeFromJson(buffer);
|
||||||
_writeToJson(buffer);
|
_writeToJson(buffer);
|
||||||
|
|
||||||
// And a convenience method to copy data from this class.
|
// And a convenience method to copy data from this class.
|
||||||
|
@ -97,12 +98,29 @@ class DataClassWriter {
|
||||||
buffer.write(');}\n');
|
buffer.write(');}\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _writeFromJson(StringBuffer buffer) {
|
||||||
|
final dataClassName = table.dartTypeName;
|
||||||
|
|
||||||
|
buffer
|
||||||
|
..write('factory $dataClassName.fromJson(Map<String, dynamic> json) {\n')
|
||||||
|
..write('return $dataClassName(');
|
||||||
|
|
||||||
|
for (var column in table.columns) {
|
||||||
|
final getter = column.dartGetterName;
|
||||||
|
final type = column.dartTypeName;
|
||||||
|
|
||||||
|
buffer.write("$getter: json['$getter'] as $type,");
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.write(');}\n');
|
||||||
|
}
|
||||||
|
|
||||||
void _writeToJson(StringBuffer buffer) {
|
void _writeToJson(StringBuffer buffer) {
|
||||||
buffer.write('Map<String, dynamic> toJson() {\n return {');
|
buffer.write('Map<String, dynamic> toJson() {\n return {');
|
||||||
|
|
||||||
for (var column in table.columns) {
|
for (var column in table.columns) {
|
||||||
final getter = column.dartGetterName;
|
final getter = column.dartGetterName;
|
||||||
buffer.write('\'$getter\': $getter,');
|
buffer.write("'$getter': $getter,");
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.write('};}');
|
buffer.write('};}');
|
||||||
|
|
Loading…
Reference in New Issue