Skip to content

Instantly share code, notes, and snippets.

@nicodoss
Last active September 1, 2022 18:25
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 nicodoss/7a778349e4340e9c95f18d3546fc726a to your computer and use it in GitHub Desktop.
Save nicodoss/7a778349e4340e9c95f18d3546fc726a to your computer and use it in GitHub Desktop.
test json file
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