From 1b79b0a133cf3f09ab558a15b84ff7e0c60bd100 Mon Sep 17 00:00:00 2001 From: Anatoly Pulyaevskiy Date: Mon, 5 Mar 2018 16:54:02 -0600 Subject: [PATCH] Allow null document snapshot data when document does not exist (#406) --- packages/cloud_firestore/CHANGELOG.md | 5 +++++ packages/cloud_firestore/lib/src/document_snapshot.dart | 4 ++++ packages/cloud_firestore/pubspec.yaml | 2 +- packages/cloud_firestore/test/cloud_firestore_test.dart | 8 ++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/cloud_firestore/CHANGELOG.md b/packages/cloud_firestore/CHANGELOG.md index f30a07e18ea5..1b561552ff3b 100644 --- a/packages/cloud_firestore/CHANGELOG.md +++ b/packages/cloud_firestore/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.2.12 + +* Fix handling of `null` document snapshots (document not exists). +* Add `DocumentSnapshot.exists`. + ## 0.2.11 * Fix Dart 2 type errors. diff --git a/packages/cloud_firestore/lib/src/document_snapshot.dart b/packages/cloud_firestore/lib/src/document_snapshot.dart index 7c12b1d21d0d..2254bef80f79 100644 --- a/packages/cloud_firestore/lib/src/document_snapshot.dart +++ b/packages/cloud_firestore/lib/src/document_snapshot.dart @@ -26,9 +26,13 @@ class DocumentSnapshot { /// Returns the ID of the snapshot's document String get documentID => _path.split('/').last; + + /// Returns `true` if the document exists. + bool get exists => data != null; } Map _asStringKeyedMap(Map map) { + if (map == null) return null; if (map is Map) { return map; } else { diff --git a/packages/cloud_firestore/pubspec.yaml b/packages/cloud_firestore/pubspec.yaml index eb19f9973f8b..377e47768756 100755 --- a/packages/cloud_firestore/pubspec.yaml +++ b/packages/cloud_firestore/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for Cloud Firestore, a cloud-hosted, noSQL database live synchronization and offline support on Android and iOS. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/cloud_firestore -version: 0.2.11 +version: 0.2.12 flutter: plugin: diff --git a/packages/cloud_firestore/test/cloud_firestore_test.dart b/packages/cloud_firestore/test/cloud_firestore_test.dart index 7068c02ddbfa..4401148bf10b 100755 --- a/packages/cloud_firestore/test/cloud_firestore_test.dart +++ b/packages/cloud_firestore/test/cloud_firestore_test.dart @@ -84,6 +84,8 @@ void main() { 'path': 'foo/bar', 'data': {'key1': 'val1'} }; + } else if (methodCall.arguments['path'] == 'foo/notExists') { + return {'path': 'foo/notExists', 'data': null}; } throw new PlatformException(code: 'UNKNOWN_PATH'); case 'Firestore#runTransaction': @@ -407,6 +409,12 @@ void main() { expect(snapshot.reference.path, equals('foo/bar')); expect(snapshot.data.containsKey('key1'), equals(true)); expect(snapshot.data['key1'], equals('val1')); + expect(snapshot.exists, isTrue); + + final DocumentSnapshot snapshot2 = + await collectionReference.document('notExists').get(); + expect(snapshot2.data, isNull); + expect(snapshot2.exists, isFalse); try { await collectionReference.document('baz').get();