0

I am trying to fetch arrays from the firestore database, but not able to do so

I am fetching other fields as this

return ListView.builder(
          itemCount: snapshot.data.documents.length,
          itemBuilder: (context, index){
            String itemTitle = snapshot.data.documents[index]['itemTitle'];
            String Name = snapshot.data.documents[index]['name'];
           
            return ListCard(Name: Name,itemTitle: itemTitle,);

          });

The array I want to fetch is in this screenshot

screenshot

How should I get it?

2 Answers 2

1

For a single value

String steps = snapshot.data.documents[index]['steps'][0]['step']

or you can iterate

List steps = [];
for (int i = 0; i < 4; i++) {
        steps.add(snapshot.data.documents[index]['steps'][i]['step'])
     }
Sign up to request clarification or add additional context in comments.

3 Comments

will it fetch whole list of the array or one particular element? I need to fetch all elements in one go
the second one will fetch each item and store a list of strings
In addition: if the number of steps is unknown at compile time: for (int i = 0; i < snapshot.data.documents[index]['steps'].length; i++) ...
0

To fetch all steps as a list, try this

return ListView.builder(
          itemCount: snapshot.data.documents.length,
          itemBuilder: (context, index){
            String itemTitle = snapshot.data.documents[index]['itemTitle'];
            String Name = snapshot.data.documents[index]['name'];
            List steps = List.castFrom(snapshot.data.documents[index]["steps"]); //this
            return ListCard(Name: Name,itemTitle: itemTitle,);

          });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.