dshukertjr
dshukertjr

Reputation: 18612

Get path of a Cloud Firestore document in Flutter

In a situation like this where I am iterating through documents inside a collection of Cloud Firestore, how can I get the path of the documents?

children: snapshot.data.documents.map((document) {
  return ListTile(
    //want to get the document path here
    title: Text(document.path),
  );

Apparently, you can access the path data, but the explanation I found on github was very unclear https://github.com/flutter/plugins/pull/244

Upvotes: 5

Views: 3061

Answers (1)

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126694

Taking a look at the source code:

In this line you can see that _path is a private property of DocumentSnapshot.

Also, you can see that the path is accessible in a DocumentReference, here.

This results in the following code:

children: snapshot.data.documents.map((document) {
  return new ListTile(
    title: new Text(document.reference.path), // this will return the path
);

Notice how I only added .reference.

Upvotes: 7

Related Questions