flutter
flutter

Reputation: 6766

How to read a firestore value in a map?

I'm trying to read the value for exp_date in my flutter app as seen in this picture:

enter image description here

The field/value pair is in a map called Card which is in a sub collection called sources . I can read the value by putting in the DocId for the sources collection(the other docId is the firebase user uid) with the following:

StreamBuilder(stream: Firestore.instance.collection('stripe_customers').document(userId).collection('sources').document('qRvDob75kTzhT3').snapshots(),builder: (context,snapshot){
                if(!snapshot.hasData){
                  return new Text("Loading");
                }
                var userDocument = snapshot.data;
                return new Text(userDocument["card"]['exp_year'].toString());
              },),

But obviously this isn't very practical. How do I access the Card map value without knowing the document id?

Upvotes: 0

Views: 1311

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

There are two ways to get the contents of a document:

  1. Know its entire, unique path.
  2. Make query against a known collection to get matching documents.

If you don't know the id required by #1, you will have to try #2. But since you don't seem to have have a way to filter the documents in that collection, you will have to fetch ALL of the documents. This is probably not what you want to do for scalability purposes.

Give some consideration about how you want to find the data in Firestore before you write the documents.

Upvotes: 1

Related Questions