Skip to content

Instantly share code, notes, and snippets.

@untillnesss
Last active January 1, 2023 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save untillnesss/4553f5964cb5d1ced3b11f0d3569e53a to your computer and use it in GitHub Desktop.
Save untillnesss/4553f5964cb5d1ced3b11f0d3569e53a to your computer and use it in GitHub Desktop.
void main(){
ExampleReq exampleReq = ExampleReq(
items: [
Item(
idProduct: 1,
isWholesaler: 1,
quantity: 12,
variants: [1,2,3],
),
Item(
idProduct: 2,
isWholesaler: 1,
quantity: 12,
variants: [32,3],
),
]
);
print(exampleReq.toJson());
}
import 'dart:convert';
class ExampleReq {
ExampleReq({
this.items = const [],
});
List<Item> items;
ExampleReq copyWith({
List<Item>? items,
}) =>
ExampleReq(
items: items ?? this.items,
);
factory ExampleReq.fromJson(String str) => ExampleReq.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory ExampleReq.fromMap(Map<String, dynamic> json) => ExampleReq(
items: json["items"] == null ? [] : List<Item>.from(json["items"].map((x) => Item.fromMap(x))),
);
Map<String, dynamic> toMap() => {
"items": List<dynamic>.from(items.map((x) => x.toMap())),
};
}
class Item {
Item({
this.idProduct,
this.quantity,
this.isWholesaler,
this.variants,
});
int? idProduct;
int? quantity;
int? isWholesaler;
List<int>? variants;
Item copyWith({
int? idProduct,
int? quantity,
int? isWholesaler,
List<int>? variants,
}) =>
Item(
idProduct: idProduct ?? this.idProduct,
quantity: quantity ?? this.quantity,
isWholesaler: isWholesaler ?? this.isWholesaler,
variants: variants ?? this.variants,
);
factory Item.fromJson(String str) => Item.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Item.fromMap(Map<String, dynamic> json) => Item(
idProduct: json["id_product"] == null ? null : json["id_product"],
quantity: json["quantity"] == null ? null : json["quantity"],
isWholesaler: json["is_wholesaler"] == null ? null : json["is_wholesaler"],
variants: json["variants"] == null ? null : List<int>.from(json["variants"].map((x) => x)),
);
Map<String, dynamic> toMap() => {
"id_product": idProduct == null ? null : idProduct,
"quantity": quantity == null ? null : quantity,
"is_wholesaler": isWholesaler == null ? null : isWholesaler,
"variants": variants == null ? "" : List<dynamic>.from(variants!.map((x) => x)),
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment