Flutterfire: [cloud_firestore] arrayUnion and arrayRemove don't accept a DocumentReference

Created on 12 Feb 2020  Â·  5Comments  Â·  Source: FirebaseExtended/flutterfire

Describe the bug
When passing a Firestore DocumentReference to an array in Firestore, using either the arrayUnion or the arrayRemove FieldValue methods, an error is produced.

The error produced in both cases::
Unhandled Exception: Invalid argument: Instance of 'DocumentReference'

Steps to reproduce
Steps to reproduce the behavior:

  1. To attempt to add a DocumentReference to an array field in a user document:
    await userDocument.reference.updateData({'favourites':FieldValue.arrayUnion([documentReference])});
  1. To attempt to remove a DocumentRefrence from an array field in a user document:
    await userDocument.reference.updateData({"favourites":FieldValue.arrayRemove([documentReference])});

Expected behavior
The "DocumentReference" type in flutter should correlate with the "reference" type available within the Firebase Console.

When using arrayUnion a documentReference should be added to an existing array (of "reference") called 'favourites' within the userDocument.

When using arrayRemove a documentReference should be removed from an existing array (of "reference") called 'favourites', if the reference exists, within the userDocument.

Additional context
Flutter Firebase versions:

firebase_core: ^0.4.3+3
cloud_firestore: ^0.13.1+1

Flutter Doctor:

flutter doctor -v
[√] Flutter (Channel beta, v1.14.6, on Microsoft Windows [Version 10.0.18362.592], locale en-GB)
    • Flutter version 1.14.6 at C:\src\flutter
    • Framework revision fabeb2a16f (2 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)

[√] VS Code (version 1.39.2)
    • VS Code at C:\Users\stuie\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.5.1

Link to my StackOverflow question on this topic:
https://stackoverflow.com/questions/60152474/can-a-firestore-documentreference-be-passed-in-an-arrayunion-method/60153407#60153407

bug

Most helpful comment

All done 😉.. waiting for a review from @ditman

All 5 comments

@stuied148 0.13.1 was a pretty major refactor of cloud_firestore.

Could you test your app with cloud_firestore: 0.13.0+2 and let us know if it works? (This is so we can narrow down this as a regression of the refactor, or a preexisting bug that carried over.)

Thanks!

_/cc @amrfarid140_

@ditman I tried using cloud_firestore: 0.13.0+2 as you suggested but again the error popped up.

I then tried reverting back to cloud_firestore: 0.12.11 and everything worked as expected. So I assume this is something to do with the refactor?

Hope this helps!

Yep it does, I'll check it out tomorrow if @amrfarid140 doesn't get to it first :P

All done 😉.. waiting for a review from @ditman

The fix for this issue is now published as: https://pub.dev/packages/cloud_firestore/versions/0.13.2+2

Was this page helpful?
0 / 5 - 0 ratings