0

Im trying to retrieve data from firestore and putting them into a list so that i can build read and build widgets from the data retrieved. I cant seem do both, i can either get the data, or append a list with a fixed value, but i CANT seem to RETRIEVE DATA + APPEND THE LIST WITH THE RETRIEVED DATA

. Sorry if im not being clear enough, do let me know what do you need, below is my screenshot from my database structure and code snippets.

Database structure : enter image description here

Data retrieval code snippet :

onRefreshPage()  {
   Firestore.instance
      .collection("testimonies")
      .getDocuments()
      .then((querySnapshot) {
    querySnapshot.documents.forEach((result) {
      print(result.data);
    });
  });
}

List declaration : List<DocumentSnapshot> testimonyCards = [];

ETCasual
  • 106
  • 11

2 Answers2

0

If I understand you correctly you want to transform the data into widgets. Have a look at FutureBuilder from Flutter: https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html and https://firebase.flutter.dev/docs/firestore/usage#realtime-changes

In your case you can do something like:

FutureBuilder<QuerySnapshot>(
  future: FirebaseFirestore.instance.collection('testimonies').get(),
  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (snapshot.hasError) {
      return Text('Something went wrong');
    }

    if (snapshot.connectionState == ConnectionState.waiting) {
      return Text("Loading");
    }

    return new ListView(
      children: snapshot.data.docs.map((DocumentSnapshot document) {
        return new ListTile(
          title: new Text(document.data()['DisplayName']),
          subtitle: new Text(document.data()['TestimonyData']),
        );
      }).toList(),
    );
  },
);
0

If I am correct, you want to query Firebase and append the results to a list. In this case, it is an array. Is this what you are looking for?

Appending data from Firebase to array

MrTech
  • 430
  • 4
  • 8