Luca_54
Luca_54

Reputation: 539

Get data from a map nested in a array in Firestore and Flutter

I have an app where I want to check if there is a contact with a certain uid.

The problem: The uid I want to check is nested in a map that is in an array.

The Firestore document looks like this:

enter image description here

I thought so far something like this:

  var data = await FirebaseFirestore.instance
    .collection("user_contacts")
    .doc(FirebaseAuth.instance.currentUser.uid)
    .get();

  if(data.data()["contacts"].contains({"uid": myUid, "date": myDate})){
    ...
  }

But I don't have the date. Is there any other way?

Thanks for help!

Upvotes: 0

Views: 475

Answers (1)

cpboyce
cpboyce

Reputation: 207

I would personally recommend that you try and stay away from arrays within your documents especially if they are going to grow to hundreds of names long. If not careful you could be pulling in unnecessary data on every call.

If you were to change your implementation I would recommend creating a subcollection.

With your current implementation you could use this function:

 const exists= (arr, val) => {
   for(let i=0; i<arr.length; i++){
        return arr[i]['uid'] == val;
   };
}

where arr is data.data()["contacts"] and val is your desired uid

Upvotes: 1

Related Questions