Last active
September 1, 2022 18:25
-
-
Save nicodoss/7a778349e4340e9c95f18d3546fc726a to your computer and use it in GitHub Desktop.
test json file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
void main() { | |
var response ='{"statut":"OK","msg":"liste des livres en base","object":[{"id":2,"titre":"Bataille pour la jeunesse","couvertureNameFile":"Couverture.jpg","livreNameFile":"Bataille pour la jeunesse.pdf","telechargement":0,"auteur":{"id":1,"name":"GBILE AKANNI"}}]}'; | |
var jsonresponse=json.decode(response); | |
print (jsonresponse["object"]); | |
List<Livres> p=(jsonresponse["object"] as List<dynamic>).map(((e)=>livresFromJson(e) as Livres)).toList(); | |
print(p); | |
} | |
class Auteur { | |
Auteur( | |
this.id, | |
this.name | |
); | |
int id; | |
String name; | |
factory Auteur.fromJson(Map<String, dynamic> json) => Auteur( | |
json["id"]??"", | |
json["name"]??"", | |
); | |
Map<String, dynamic> toJson() => { | |
"id": id, | |
"name": name, | |
}; | |
} | |
Livres livresFromJson(String str) => Livres.fromJson(json.decode(str)); | |
String livresToJson(Livres data) => json.encode(data.toJson()); | |
class Livres { | |
Livres( this.id,this.titre,{this.couvertureNameFile,this.livreNameFile,this.telechargement,this.auteur}); | |
int id; | |
String titre; | |
String ?couvertureNameFile; | |
String ? livreNameFile; | |
int ?telechargement; | |
Auteur ? auteur; | |
@override | |
String toString() => titre; | |
bool isEqual(Livres model) { | |
return id == model.id; | |
} | |
factory Livres.fromJson(Map<String, dynamic> json) => Livres( | |
json["id"],json["titre"], | |
couvertureNameFile: json["couvertureNameFile"] ?? "null", | |
livreNameFile: json["livreNameFile"] ?? "null", | |
telechargement: json["telechargement"] ?? "null", | |
auteur: json["auteur"] == null ? null : Auteur.fromJson(json["auteur"]) | |
); | |
Map<String, dynamic> toJson() => { | |
"id": id, | |
"titre": titre, | |
"couvertureNameFile": couvertureNameFile ?? "null", | |
"livreNameFile": livreNameFile ?? "null", | |
"telechargement": telechargement ?? "null", | |
"auteur" : auteur?.toJson()??"", | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment