Take the size of the firestore array to use in Listview.Builder itemcount

Asked

Viewed 17 times

-1

return Scaffold(
  body: StreamBuilder(
    stream: stream,
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        return ListView.builder(
          itemCount: snapshot.data.docs.length,
          itemBuilder: (context, index) {
            var teste = snapshot.data.docs[index].data()['romaneioItens']
                [index]['cliente'];
            var teste1 = snapshot.data.docs[index].data()['romaneioItens']
                [index]['enderecoEntrega'];
            return ListTile(
              title: Text(
                teste['NomeFantasia'],
              ),
              subtitle: Column(
                children: [
                  Row(
                    children: [
                      Text(teste1['logradouro'] ?? ''),
                      Text(teste1['numero'] ?? ''),
                      Text(teste1['bairro'] ?? ''),
                    ],
                  ),
                  Row(
                    children: [
                      Text(teste1['cidade'] ?? ''),
                      Text(teste1['UF'] ?? ''),
                    ],
                  ),
                  Row(
                    children: [
                      Text(
                          snapshot.data.docs[index].data()['obsEnt'] ?? ''),
                    ],
                  ),
                ],
              ),
            );
          },
        );
      } else {
        return Container();
      }
    },
  ),
);

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

My code is as above but in my itemcount when I give a print, it shows me 2 - and takes the first array value of every doc I have in my Collection in case I have two Docs - however, in my array there are 4 items. How do I get this value and pass in itemcount to correctly display the 4 items in my array?

1 answer

0

If you need the amount of items to be equal to the amount of your array stored in the firestore you can do.

return Scaffold(
  body: StreamBuilder(
    stream: stream,
    builder: (context, snapshot) {
      if (snapshot.hasData) {

       List<dynamic> lista = snapshot.data.docs[index].data()['nomeDoArray']

        return ListView.builder(
          itemCount: lista.length,
          itemBuilder: (context, index) {
            var teste = snapshot.data.docs[index].data()['romaneioItens']
                [index]['cliente'];
            var teste1 = snapshot.data.docs[index].data()['romaneioItens']
                [index]['enderecoEntrega'];
            return ListTile(
              title: Text(
                teste['NomeFantasia'],
              ),
              subtitle: Column(
                children: [
                  Row(
                    children: [
                      Text(teste1['logradouro'] ?? ''),
                      Text(teste1['numero'] ?? ''),
                      Text(teste1['bairro'] ?? ''),
                    ],
                  ),
                  Row(
                    children: [
                      Text(teste1['cidade'] ?? ''),
                      Text(teste1['UF'] ?? ''),
                    ],
                  ),
                  Row(
                    children: [
                      Text(
                          snapshot.data.docs[index].data()['obsEnt'] ?? ''),
                    ],
                  ),
                ],
              ),
            );
          },
        );
      } else {
        return Container();
      }
    },
  ),
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.