diff --git a/packages/cloud_firestore/cloud_firestore/CHANGELOG.md b/packages/cloud_firestore/cloud_firestore/CHANGELOG.md index e0b8f7590e85..e50dfa3906f4 100644 --- a/packages/cloud_firestore/cloud_firestore/CHANGELOG.md +++ b/packages/cloud_firestore/cloud_firestore/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.13.2+2 + +* Fixed crashes when using `FieldValue.arrayUnion` & `FieldValue.arrayRemove` with `DocumentReference` objects + ## 0.13.2+1 * Add Web integration documentation to README. diff --git a/packages/cloud_firestore/cloud_firestore/example/test_driver/cloud_firestore.dart b/packages/cloud_firestore/cloud_firestore/example/test_driver/cloud_firestore.dart index 85b31b057c3e..76b37fe67fda 100644 --- a/packages/cloud_firestore/cloud_firestore/example/test_driver/cloud_firestore.dart +++ b/packages/cloud_firestore/cloud_firestore/example/test_driver/cloud_firestore.dart @@ -434,5 +434,44 @@ void main() { final DocumentSnapshot actual = results[0]; expect(actual.documentID, 'test-docRef'); }); + + test('FieldValue.arrayUnion with DocumentRefrence', () async { + final CollectionReference ref = firestore.collection('messages'); + await ref.document('test-docRef-1').setData({"message": "1"}); + await ref.document('test-docRef-2').setData({"message": "2"}); + + await ref.document('test-docRef').setData({ + 'children': [ + ref.document("test-docRef-1"), + ], + }); + + await ref.document("test-docRef").updateData({ + 'children': FieldValue.arrayUnion([ref.document('test-docRef-2')]) + }); + + final DocumentSnapshot snapshot = await ref.document("test-docRef").get(); + expect(snapshot.data['children'].length, equals(2)); + }); + + test('FieldValue.arrayRemove with DocumentRefrence', () async { + final CollectionReference ref = firestore.collection('messages'); + await ref.document('test-docRef-1').setData({"message": "1"}); + await ref.document('test-docRef-2').setData({"message": "2"}); + + await ref.document('test-docRef').setData({ + 'children': [ + ref.document("test-docRef-1"), + ref.document("test-docRef-2"), + ], + }); + + await ref.document("test-docRef").updateData({ + 'children': FieldValue.arrayRemove([ref.document('test-docRef-2')]) + }); + + final DocumentSnapshot snapshot = await ref.document("test-docRef").get(); + expect(snapshot.data['children'].length, equals(1)); + }); }); } diff --git a/packages/cloud_firestore/cloud_firestore/lib/src/field_value.dart b/packages/cloud_firestore/cloud_firestore/lib/src/field_value.dart index e278ef651d50..4e1fb4f05940 100644 --- a/packages/cloud_firestore/cloud_firestore/lib/src/field_value.dart +++ b/packages/cloud_firestore/cloud_firestore/lib/src/field_value.dart @@ -26,7 +26,7 @@ class FieldValue extends platform.FieldValuePlatform { /// will be overwritten with an array containing exactly the specified /// elements. static FieldValue arrayUnion(List elements) => - FieldValue._(_factory.arrayUnion(elements)); + FieldValue._(_factory.arrayUnion(_CodecUtility.valueEncode(elements))); /// Returns a special value that tells the server to remove the given /// elements from any array value that already exists on the server. @@ -35,7 +35,7 @@ class FieldValue extends platform.FieldValuePlatform { /// If the field being modified is not already an array it will be overwritten /// with an empty array. static FieldValue arrayRemove(List elements) => - FieldValue._(_factory.arrayRemove(elements)); + FieldValue._(_factory.arrayRemove(_CodecUtility.valueEncode(elements))); /// Returns a sentinel for use with update() to mark a field for deletion. static FieldValue delete() => FieldValue._(_factory.delete()); diff --git a/packages/cloud_firestore/cloud_firestore/pubspec.yaml b/packages/cloud_firestore/cloud_firestore/pubspec.yaml index 0e5fa833a464..febf3e4b49ec 100755 --- a/packages/cloud_firestore/cloud_firestore/pubspec.yaml +++ b/packages/cloud_firestore/cloud_firestore/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for Cloud Firestore, a cloud-hosted, noSQL database with live synchronization and offline support on Android and iOS. homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/cloud_firestore/cloud_firestore -version: 0.13.2+1 +version: 0.13.2+2 flutter: plugin: