-2

I have a list of documents and each document has a field of a string array named "fav", it has more than 50k emails, there are almost 1000 documents and in each document's "fav" array has variable length including 50k, 20k,10, etc. I was fetching all documents

 Firestore.instance.collection("save").snapshots();

through StreamBuilder

StreamBuilder(
      stream: Firestore.instance.collection("save").snapshots();,
      builder: (context, snapshot) {
        if (!snapshot.hasData)
          return Text("Loading Data.............");
        else {
          listdata = snapshot.data.documents;

          return _buildBody(snapshot.data.documents);
        }
      },
    )

Now How I can search my required email from each document's field "fav"? I have to perform an operation after finding the required id in the array locally.

field "fav" can have 50k items

Saxtheowl
  • 4,136
  • 5
  • 23
  • 32
M.ArslanKhan
  • 3,640
  • 8
  • 34
  • 56
  • 2
    Question isn't clear, can you add more details. If you want to fetch all the items where `fav` contains `abc@gmail.com` then you can do something like `.where("fav", arrayContains: "abc@gmail.com")`. But if you have those many emails (like in thousands), better to use collections for storing emails. – Chenna Reddy Sep 30 '19 at 07:51
  • Question related to that: https://stackoverflow.com/questions/51924896/firestore-flutter-array-contains – Francis Rodrigues Dec 18 '19 at 23:32
  • It was fixed on Github issue. I strongly recommend you to check the Flutter Github repository before asking for help here. ;) – Francis Rodrigues Dec 18 '19 at 23:34

2 Answers2

1

The question is not very clear, but for my understanding, this is what you are looking for

Firestore.instance.collection('save')
.where('fav', arrayContains: 'abc@gmail.com').snapshots()
  • In current version you should implement this way: `where(string, whereFilterOp, value)`. Check it out here: https://github.com/flutter/flutter/issues/20489#issuecomment-419241219 – Francis Rodrigues Dec 18 '19 at 23:37
0

The question is not very clear, but for my understanding, you want to find one e-mail in the array field. This array is contained on each document, and all the documents are "streamed" in a collection of snapshots.

Contains Method: https://api.dartlang.org/stable/2.0.0/dart-core/Iterable/contains.html

bool contains (
Object element
)

Returns true if the collection contains an element equal to element.

This operation will check each element in order for being equal to element, unless it has a more efficient way to find an element equal to element.

The equality used to determine whether element is equal to an element of the iterable defaults to the Object.== of the element.

Some types of iterable may have a different equality used for its elements. For example, a Set may have a custom equality (see Set.identity) that its contains uses. Likewise the Iterable returned by a Map.keys call should use the same equality that the Map uses for keys.

Implementation

bool contains(Object element) {
  for (E e in this) {
    if (e == element) return true;
  }
  return false;
}
Mario
  • 406
  • 2
  • 6