William Nguyen
William Nguyen

Reputation: 21

How to loop through a DocumentSnapshot.data and retrieve id's and keys?

How do I loop through a DocumentSnapshot.data() object and retrieve its keys and values? My code currently looks like this:

Future<void> drawPolygons() async {
    var points = await FirebaseFirestore.instance
        .collection('polygons')
        .doc(uid)
        .get()
        .then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        //if user has polygon data on account
        var data = documentSnapshot.data();
      }
    });
  }

Upvotes: 0

Views: 198

Answers (1)

MANISH DAYMA
MANISH DAYMA

Reputation: 1242

**Update Your Method and Convert the data to Map**
    
    
    Future<void> drawPolygons() async {
        var points = await FirebaseFirestore.instance
            .collection('polygons')
            .doc(uid)
            .get()
            .then((DocumentSnapshot documentSnapshot) {
          if (documentSnapshot.exists) {
          Map data = (documentSnapshot.data() as Map);
          for (var entry in data.values) {
             print(entry);
           }
          }
        });
      }

Upvotes: 2

Related Questions