jastor_007
jastor_007

Reputation: 441

Flutter Firebase: get documents where a particular fiels is not null

I am trying to fetch documents where callerId fields does not contain null value

I tries this

final collection =
        _firestoreService.instance.collection(TIME_SLOTS_COLLECTION_NAME);

Query query = collection
  .where("listenerId", isEqualTo: _userService.user!.firestoreId!)
  .where("callerId", isNull: false)
  .orderBy("startTime", descending: true);

print("after query");

But it returns nothing. Code after this statement does not run at all. It means after query does not get printed on the console. I am not sure what's the problem?

I tried this

final collection =
        _firestoreService.instance.collection(TIME_SLOTS_COLLECTION_NAME);

Query query = collection
  .where("listenerId", isEqualTo: _userService.user!.firestoreId!)
  .where("callerId", isEqualTo: "caller1")
  .orderBy("startTime", descending: true);

print("after query");

It runs but the first one does not. Does anyone know something about this?

Upvotes: 2

Views: 2236

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

From looking at the FlutterFire implementation of isNull:

if (isNull != null) {
  assert(
      isNull,
      'isNull can only be set to true. '
      'Use isEqualTo to filter on non-null values.');
  addCondition(field, '==', null);
}

So it looks like your condition is invalid, and you should be seeing an error message in your program output. That also explains why the app stops working: the assertion fails, and thus your app crashes.

You can use isNotEqualTo (or >, >= and others) to filter here:

.where("callerId", isNotEqualTo: false)

What I find really helpful in cases such as this is to keep the table of supported data types and their sort order handy.

Upvotes: 3

Related Questions