[SOLVED] Assemble data list to send between screens


This Content is from Stack Overflow. Question asked by Lucas Barreto Luz

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:

image here

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 velors in a class is to send that class later to another screen, using Navigator.pish.


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))

This Question was asked in StackOverflow by Lucas Barreto Luz and Answered by TripleNine It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?