I'm having trouble putting together a list of a class I created by taking information from an AsyncSnapshot.
My code is using a FutureBuilder and getting a dynamic list of items as shown in the image below:
So I created the following class to hold these values:
class TesteListaTopicos {
int? id;
int? idVideo;
String? titulo;
String? tipoMidia;
String? tipoMidiaDetalhe;
TesteListaTopicos({this.id, this.idVideo, this.tipoMidia,
this.tipoMidiaDetalhe, this.titulo});
}
I want to create a list of TesteListaTopicos:
var topicosList = List<TesteListaTopicos>;
But I don't know how to pass the values to the list. I access the snapshot values as follows:
String title = (snapshot.data.listaTopicoVideo[index].titulo);
int id = (snapshot.data.listaTopicoVideo[index].id);
int idVideo = (snapshot.data.listaTopicoVideo[index].idVideo);
Could someone help me how do I store the values in the class? My goal with storing all values in a class and to send that class later to another screen, using Navigator.push.
CodePudding user response:
Usually you have a fromJson
constructor or method which converts the Json to the TesteListaTopicos
object like this:
TesteListaTopicos.fromJson(Map<String, dynamic> json) :
id = json["id"],
idVideo = json["idVideo"],
tipoMidia = json["tipoMidia"],
tipoMidiaDetalhe = json["tipoMidiaDetalhe"],
titulo = json["titulo"];
Then you can map the list listaTopicoVideo
to a list of TesteListaTopicos
like this:
List<TesteListaTopicos> topicosList = snapshot.data.listaTopicoVideo
.map((element) => TesteListaTopicos.fromJson(element))
.toList();