0

I want to create a column of containers that have text on them based on the fields in my Database, however, both forEach and Map only works on collections. How do I go through the fields in a single document?

This gives: "The method 'map' isn't defined for the type 'Object'."

  FutureBuilder(
          future:
        FirebaseFirestore.instance.collection("posts")
          .doc(this.id).collection("tags").doc("tags").get(),

          builder: (context, snapshot)
          {
            return Column(children: snapshot.data!.map((e)=> Container(child: Text(e))).toList());

          };
          
      ),
Jocko J
  • 1
  • 1
  • https://stackoverflow.com/questions/53629646/flutter-iterate-through-object-keys-and-values – Doug Stevenson Jul 30 '21 at 17:47
  • @DougStevenson So basically, it doesn't work unless I import an serializable class? I guess I'll have to split the fields into documents then. – Jocko J Jul 30 '21 at 18:11

1 Answers1

2

You are fetching a single document and hence the snapshot is a DocumentSnapshot which as a data() method to get document's data and not docs which returns an array of documents in a QuerySnapshot:

Map<String, dynamic> data = snapshot.data() as Map<String, dynamic>;

for (String key in data.keys){
  print(key + data[key]!);
}
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84