der
der

Reputation: 89

Unable to retrieve Document ID

I have reviewed and tried multiple ways of retrieving the document id for each document data, but I am unable to do so. Please help.

final _domainPost = FirebaseFirestore.instance.collection('developmentdomain');

                 StreamBuilder(
                    stream: _domainPost.snapshots(),
                    builder: (BuildContext context,
                        AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (!snapshot.hasData)
                        return new Text("There are no records.");
                      if (snapshot.connectionState == ConnectionState.waiting)
                        return Center(child: CircularProgressIndicator());
                      List devDomainList = snapshot.data!.docs;

                      List<DevelopmentDomain> _records = devDomainList
                          .map(
                            (devDomainPost) => DevelopmentDomain(
                                
                                title: devDomainPost['title'],
                                description: devDomainPost['description']),
                          )
                          .toList();
                      return ListView.builder(
                        padding:
                            const EdgeInsets.fromLTRB(20.0, 20.0, 20.0, 60.0),
                        itemCount: snapshot.data!.size,
                        itemBuilder: (context, index) {
                          return DevelopmentDomainCard(
                            developmentDomainRecord: _records[index],
                            isAdmin: _isAdmin,
                          );
                        },
                      );
                    },
                  )

Upvotes: 0

Views: 41

Answers (1)

Code on the Rocks
Code on the Rocks

Reputation: 17624

Each DocumentSnapshot in snapshot.data!.docs will have an id property that contains the Document ID from Firestore.

In your case, devDomainPost.id should have it inside your map.

Upvotes: 2

Related Questions