Iterate over a Firestore Document in Flutter

Viewed 1269

I have this code snippet and I wonder if there's a way to iterate over a firestore document to get their fields names and consequently their values. My goal is to not worry with the code so that I can manipulate each document in firebase store however I want.

Thanks in advance!

  Widget MainBody(){

    return SingleChildScrollView(
      child: StreamBuilder(
        stream: firestore.collection('painel').snapshots(),
        builder: (context, snapshot){


          if(docNumber != null){
          if (!snapshot.hasData) return Text('Carregando');
          return ListView(
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('Campo 1')
                  ),
                  Expanded(
                      flex: 3,
                      child: Text(snapshot.data.documents[docNumber]['campo1']) //change campo1 with some iterable
                  ),
                       .
                       .
                       .
                  Expanded(
                    flex: 1,
                    child: Text('Campo n')
                  ),
                  Expanded(
                      flex: 3,
                      child: Text(snapshot.data.documents[docNumber]['campon'])
                  ),
                ],
              )
            ],
          );
          }else{
            return Text('Por favor, escaneie');
          }
        }
      )
    );
  }

EDIT: I just figure out how to do it, here's the code snippet. Any improvements I would be glad to hear. cya!

  Widget MainBody() {
    return SingleChildScrollView(
      child: StreamBuilder(
          stream: Firestore.instance.collection('painel').snapshots(),
          builder: (context, snapshot) {

            if (docNumber != null){

             var docData = snapshot.data.documents[docNumber].data; 
             int docLen = docData.length;

             if (!snapshot.hasData) return Text('Loading');
              return ListView.builder(
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  itemCount: docLen,
                  itemBuilder: (context,index) =>
                      buildItem(context, docData.keys.elementAt(index), docData.values.elementAt(index))
              );
            } else {
              return Text('Not found.');
            }
          }
      ),
    );
  }

    Widget buildItem(BuildContext context, documentKey, documentValue){
    return Row(
      children: <Widget>[
        Expanded(
          flex: 1,
          child: Text(documentKey)
        ),
        Expanded(
            flex: 3,
            child: Text(documentValue)
        )
      ],
    );
  }
1 Answers

To build a listview, in this case you can use Listview.Builder() by iterating documents. You can iterate through your documents as display below.

final docs =snapshot.data.documents;
return ListView.builder(
            scrollDirection: Axis.vertical,
            shrinkWrap: true,
            itemCount: docs.length,
            itemBuilder: (ctx, index)=>
              Row(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Text('Campo 1')
                  ),
                  Expanded(
                      flex: 3,
                      child: Text(docs[index]['campo1']) //change campo1 with some iterable
                  ),
                       .
                       .
                       .
                  Expanded(
                    flex: 1,
                    child: Text('Campo n')
                  ),
                  Expanded(
                      flex: 3,
                      child: Text(docs[index]['campon'])
                  ),
                ],
              )
          );
Related