Skip to content

Instantly share code, notes, and snippets.

@eduardoflorence
Created December 28, 2020 20:53
Show Gist options
  • Save eduardoflorence/e5fe260ebc308cc95d844878ca5274a6 to your computer and use it in GitHub Desktop.
Save eduardoflorence/e5fe260ebc308cc95d844878ca5274a6 to your computer and use it in GitHub Desktop.
Dart - List with submodel
Future main() async {
Map<String, dynamic> res = {
"content": [
{
"from_info": {
"address": {
"address_string": "Av. Paulista, 302 - Bela Vista, São Paulo - SP, 01310-000, Brazil",
"latitude": "-23.5688619",
"longitude": "-46.6464484"
},
"logo": "https://s3.eu-central-1.amazonaws.com/engine.izi.finance/izifood/stores/1605703676787.png",
"sender_id": "5fb514911d03250013a0aa13",
"sender_name": "Test Resturant"
},
"to_info": {
"address": {
"address_string": "Av. Paulista, 402 - Bela Vista, São Paulo - SP, 01310-000, Brazil",
"latitude": "-23.5685597",
"longitude": "-46.6474527"
},
"receiver_id": "5f80af8b488c99001aa53a0a",
"receiver_name": "gabriel sales bezerra"
},
"location_order": {
"type": "Point",
"coordinates": [
-23.5688619,
-46.6464484
]
},
"type_order": 1,
"order_status": 1,
"package": [
{
"id": "5fb516431d03250013a0aa14",
"qtd": 8,
"value": 1,
"description": "Pizza de Champion "
}
],
"_id": "5fb6b10c0ba89b000758a9ab",
"origin_id": "5fb6b0f712c2a80013280fce",
"order_hash": "000024",
"delivery_value": "6.21",
"createdAt": "2020-11-19T17:53:16.491Z",
"updatedAt": "2020-11-19T17:53:16.491Z",
"__v": 0
}
]
};
// Exemplo do que vai ser feito no decoder
final List<Deliveries> delivery = List<Deliveries>.from(res['content'].map((delivery) => Deliveries.fromJson(delivery)));
print(delivery[0].typeOrder);
print(delivery[0].fromInfo.logo);
}
class Deliveries {
Deliveries({
this.fromInfo,
this.toInfo,
this.locationOrder,
this.typeOrder,
this.orderStatus,
this.package,
this.id,
this.originId,
this.orderHash,
this.deliveryValue,
this.createdAt,
this.updatedAt,
this.v,
});
FromInfo fromInfo;
ToInfo toInfo;
LocationOrder locationOrder;
int typeOrder;
int orderStatus;
List<Package> package;
String id;
String originId;
String orderHash;
String deliveryValue;
DateTime createdAt;
DateTime updatedAt;
int v;
factory Deliveries.fromJson(Map<String, dynamic> json) => Deliveries(
fromInfo: FromInfo.fromJson(json["from_info"]),
toInfo: ToInfo.fromJson(json["to_info"]),
locationOrder: LocationOrder.fromJson(json["location_order"]),
typeOrder: json["type_order"],
orderStatus: json["order_status"],
package: List<Package>.from(json["package"].map((x) => Package.fromJson(x))),
id: json["_id"],
originId: json["origin_id"],
orderHash: json["order_hash"],
deliveryValue: json["delivery_value"],
createdAt: DateTime.parse(json["createdAt"]),
updatedAt: DateTime.parse(json["updatedAt"]),
v: json["__v"],
);
Map<String, dynamic> toJson() => {
"from_info": fromInfo.toJson(),
"to_info": toInfo.toJson(),
"location_order": locationOrder.toJson(),
"type_order": typeOrder,
"order_status": orderStatus,
"package": List<dynamic>.from(package.map((x) => x.toJson())),
"_id": id,
"origin_id": originId,
"order_hash": orderHash,
"delivery_value": deliveryValue,
"createdAt": createdAt.toIso8601String(),
"updatedAt": updatedAt.toIso8601String(),
"__v": v,
};
}
class FromInfo {
FromInfo({
this.address,
this.logo,
this.senderId,
this.senderName,
});
Address address;
String logo;
String senderId;
String senderName;
factory FromInfo.fromJson(Map<String, dynamic> json) => FromInfo(
address: Address.fromJson(json["address"]),
logo: json["logo"],
senderId: json["sender_id"],
senderName: json["sender_name"],
);
Map<String, dynamic> toJson() => {
"address": address.toJson(),
"logo": logo,
"sender_id": senderId,
"sender_name": senderName,
};
}
class Address {
Address({
this.addressString,
this.latitude,
this.longitude,
});
String addressString;
String latitude;
String longitude;
factory Address.fromJson(Map<String, dynamic> json) => Address(
addressString: json["address_string"],
latitude: json["latitude"],
longitude: json["longitude"],
);
Map<String, dynamic> toJson() => {
"address_string": addressString,
"latitude": latitude,
"longitude": longitude,
};
}
class LocationOrder {
LocationOrder({
this.type,
this.coordinates,
});
String type;
List<double> coordinates;
factory LocationOrder.fromJson(Map<String, dynamic> json) => LocationOrder(
type: json["type"],
coordinates: List<double>.from(json["coordinates"].map((x) => x.toDouble())),
);
Map<String, dynamic> toJson() => {
"type": type,
"coordinates": List<dynamic>.from(coordinates.map((x) => x)),
};
}
class Package {
Package({
this.id,
this.qtd,
this.value,
this.description,
});
String id;
int qtd;
int value;
String description;
factory Package.fromJson(Map<String, dynamic> json) => Package(
id: json["id"],
qtd: json["qtd"],
value: json["value"],
description: json["description"],
);
Map<String, dynamic> toJson() => {
"id": id,
"qtd": qtd,
"value": value,
"description": description,
};
}
class ToInfo {
ToInfo({
this.address,
this.receiverId,
this.receiverName,
});
Address address;
String receiverId;
String receiverName;
factory ToInfo.fromJson(Map<String, dynamic> json) => ToInfo(
address: Address.fromJson(json["address"]),
receiverId: json["receiver_id"],
receiverName: json["receiver_name"],
);
Map<String, dynamic> toJson() => {
"address": address.toJson(),
"receiver_id": receiverId,
"receiver_name": receiverName,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment