Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter firestore compound query

i'd like to know if there's a way to query firebase firestore collection in Flutter adding more than one field to the query. I tried some ways but i didn't realize how to do that. For example:

CollectionReference col = Firestore.instance
    .collection("mycollection");

col.where('nome', isEqualTo: 'Tyg');
col.where('valor', isLessThan: '39');

Someone, please, have some way to do that? i am new in flutter and not getting the way.

like image 526
Jorge Vieira Avatar asked May 13 '18 13:05

Jorge Vieira


People also ask

How do I check if a collection exists in firestore Flutter?

So to know if a Collection exists of no, you can run a query that checks for a field in Info Documents (eg CollectionInfo. exists) to know which Collections have been already created. However, please keep in mind that this will have an extra cost every extra Read/Write operation.

What is FlutterFire?

FlutterFire is a set of Flutter plugins which connect your Flutter application to Firebase.

What is QuerySnapshot Flutter?

A QuerySnapshot contains the results of a query. It can contain zero or more DocumentSnapshot objects. Subclassing Note: Cloud Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.


1 Answers

Building Firestore queries follows a "builder pattern". Every time you call where it returns a new query object. So the code you have constructs two queries, that you don't assign to anything.

CollectionReference col = Firestore.instance
    .collection("mycollection");

Query nameQuery = col.where('nome', isEqualTo: 'Tyg');
Query nameValorQuery = nameQuery.where('valor', isLessThan: '39');
like image 113
Frank van Puffelen Avatar answered Nov 07 '22 05:11

Frank van Puffelen