user13410550
user13410550

Reputation:

How should I fetch the array from firestore database in flutter?

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?

Upvotes: 0

Views: 646

Answers (2)

king mort
king mort

Reputation: 1473

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'])
     }

Upvotes: 1

ByteMe
ByteMe

Reputation: 1670

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

          });

Upvotes: 0

Related Questions