Skip to content

Commit

Permalink
Allow null document snapshot data when document does not exist (flutt…
Browse files Browse the repository at this point in the history
  • Loading branch information
pulyaevskiy authored and kroikie committed Mar 5, 2018
1 parent dc51f5a commit 1b79b0a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/cloud_firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
4 changes: 4 additions & 0 deletions packages/cloud_firestore/lib/src/document_snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> _asStringKeyedMap(Map<dynamic, dynamic> map) {
if (map == null) return null;
if (map is Map<String, dynamic>) {
return map;
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/cloud_firestore/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/cloud_firestore
version: 0.2.11
version: 0.2.12

flutter:
plugin:
Expand Down
8 changes: 8 additions & 0 deletions packages/cloud_firestore/test/cloud_firestore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ void main() {
'path': 'foo/bar',
'data': <String, dynamic>{'key1': 'val1'}
};
} else if (methodCall.arguments['path'] == 'foo/notExists') {
return <String, dynamic>{'path': 'foo/notExists', 'data': null};
}
throw new PlatformException(code: 'UNKNOWN_PATH');
case 'Firestore#runTransaction':
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1b79b0a

Please sign in to comment.