Flutterfire: [cloud_firestore] List.contains doesn't compare DocumentReference correctly

Created on 19 Feb 2020  Â·  6Comments  Â·  Source: FirebaseExtended/flutterfire

Describe the bug
The List.compare() method always returns false when comparing a DocumentReference with List<DocumentReference>. This bug doesn't produce an error in console, but it's effects are visible when certain code can't be reached.
This bug exists in cloud_firestore: 0.13.0 and above.
This bug doesn't exist in cloud_firestore: 0.12.11.

To Reproduce
Steps to reproduce the behavior:

  1. Create a List<DocumentReference> from firestore data.
  2. Pass a valid DocumentReference into the List.contains() method.
  3. 3.
List<DocumentReference> favorites = List.from(userDocument['favorites']);
        if (favorites.contains(documentReference)) {
          // This is never reached
        }
  1. The List.contains() method always returns false, even if the list contains the DocumentReference. There is no error thrown.

Expected behavior
The List.compare() method should return true if the List<DocumentReference> contains the passed DocumentReference. It should only return false if the List doesn't contain the DocumentReference.

Additional context
Firebase versions:

firebase_core: ^0.4.4
cloud_firestore: ^0.13.2+2

Flutter doctor:

[√] Flutter (Channel beta, v1.14.6, on Microsoft Windows [Version 10.0.18362.657], locale en-GB)
    • Flutter version 1.14.6 at C:\src\flutter
    • Framework revision fabeb2a16f (3 weeks ago), 2020-01-28 07:56:51 -0800
    • Engine revision c4229bfbba
    • Dart version 2.8.0 (build 2.8.0-dev.5.0 fc3af737c7)


[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\stuie\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.

[√] Android Studio (version 3.5)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 43.0.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
crowd cloud_firestore regression bug

Most helpful comment

Thanks @stuied148 @leetworx

I am suspecting that this is happening because we delegate all functions to the method channel implementation delegate under the hood. A fix for that might be delegation equal operations to method channel implementation delegate as well to make this work.

Will give it a try soon and update this issue.

All 6 comments

I just came across this as well upon upgrading to 0.13.3. I see that recently in the migration to support platform interfaces a change was made to the hashcode implementation of DocumentReference, but not the == override, that seems perhaps suspicious.

FYI as a workaround, I was able to use:

return list.any((DocumentReference ref) {
  return ref.documentID == id;
});

but this is far from ideal.

Just checked and sure enough it does seem like equality is broken - for example:

given a DocumentReference 'userRef' that looks like: users/GUID where GUID is the documentID

userRef == userRef // always returns false
userRef.documentID == GUID // valid equality check

It didn't even occur to me to check the equality operators, good find @leetworx

Hopefully @ditman and/or @amrfarid140 can help with this.

Thanks @stuied148 @leetworx

I am suspecting that this is happening because we delegate all functions to the method channel implementation delegate under the hood. A fix for that might be delegation equal operations to method channel implementation delegate as well to make this work.

Will give it a try soon and update this issue.

Operator == is being added to FieldValues: https://github.com/FirebaseExtended/flutterfire/pull/2060

It might serve as an inspiration for this one @amrfarid140

Hey all, as part of our on-going work for #2582, this has been resolved in our Firebase Firestore rework (#2913) - which has been merged into master. We'll look at publishing some prereleases in the next few days. Thank you

Was this page helpful?
0 / 5 - 0 ratings