Skip to content

Commit b00d25c

Browse files
Kyle Bradshawcollinjackson
authored andcommitted
Create more convenience methods for Firestore (flutter#308)
* Create an `add` method for CollectionReference. This convenience method helps the Flutter API match Firebase's Android API. Based on user feedback. * nit - remove a space * Match DocumentSnapshot's documentID method for convenience. * Typo in docs * Double the typo double the fun * Update pubspec.yaml * Update CHANGELOG.md
1 parent 232c902 commit b00d25c

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

packages/cloud_firestore/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.4
2+
3+
* Support for DocumentReference.documentID
4+
* Support for CollectionReference.add
5+
16
## 0.2.3
27

38
* Simplified and upgraded Android project template to Android SDK 27.

packages/cloud_firestore/lib/src/collection_reference.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CollectionReference extends Query {
2626
///
2727
/// If no [path] is provided, an auto-generated ID is used.
2828
///
29-
/// The unique key generated by is prefixed with a client-generated timestamp
29+
/// The unique key generated is prefixed with a client-generated timestamp
3030
/// so that the resulting list will be chronologically-sorted.
3131
DocumentReference document([String path]) {
3232
List<String> childPath;
@@ -39,6 +39,17 @@ class CollectionReference extends Query {
3939
}
4040
return new DocumentReference._(_firestore, childPath);
4141
}
42+
43+
/// Returns a `DocumentReference` with an auto-generated ID, after
44+
/// populating it with provided [data].
45+
///
46+
/// The unique key generated is prefixed with a client-generated timestamp
47+
/// so that the resulting list will be chronologically-sorted.
48+
Future<DocumentReference> add(Map<String, dynamic> data) async {
49+
final DocumentReference newDocument = document();
50+
await newDocument.setData(data);
51+
return newDocument;
52+
}
4253
}
4354

4455
class ServerValue {

packages/cloud_firestore/lib/src/document_reference.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class DocumentReference {
2222
/// Slash-delimited path representing the database location of this query.
2323
String get path => _pathComponents.join('/');
2424

25+
/// This document's given or generated ID in the collection.
26+
String get documentID => _pathComponents.last;
27+
2528
/// Writes to the document referred to by this [DocumentReference]. If the
2629
/// document does not yet exist, it will be created. If you pass [SetOptions],
2730
/// the provided data will be merged into an existing document.

packages/cloud_firestore/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Cloud Firestore, a cloud-hosted, noSQL database
33
live synchronization and offline support on Android and iOS.
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/cloud_firestore
6-
version: 0.2.3
6+
version: 0.2.4
77

88
flutter:
99
plugin:

0 commit comments

Comments
 (0)