Skip to content

Commit

Permalink
Breaking changes to signature of snapshots and setData in cloud_fires…
Browse files Browse the repository at this point in the history
…tore (flutter#536)

These changes are required to support the metadata argument to snapshots and other future API changes.
  • Loading branch information
collinjackson authored May 7, 2018
1 parent 796c9ca commit 4f7ec09
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 49 deletions.
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.7.0

* **Breaking change**. `snapshots` is now a method instead of a getter.
* **Breaking change**. `setData` uses named arguments instead of `SetOptions`.

## 0.6.3

* Updated Google Play Services dependencies to version 15.0.0.
Expand Down
2 changes: 1 addition & 1 deletion packages/cloud_firestore/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MessageList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new StreamBuilder<QuerySnapshot>(
stream: firestore.collection('messages').snapshots,
stream: firestore.collection('messages').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return const Text('Loading...');
final int messageCount = snapshot.data.documents.length;
Expand Down
1 change: 0 additions & 1 deletion packages/cloud_firestore/lib/cloud_firestore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ part 'src/firestore.dart';
part 'src/geo_point.dart';
part 'src/query.dart';
part 'src/query_snapshot.dart';
part 'src/set_options.dart';
part 'src/firestore_message_codec.dart';
part 'src/snapshot_metadata.dart';
part 'src/transaction.dart';
Expand Down
15 changes: 9 additions & 6 deletions packages/cloud_firestore/lib/src/document_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@ class DocumentReference {
/// This document's given or generated ID in the collection.
String get documentID => _pathComponents.last;

/// Writes to the document referred to by this [DocumentReference]. If the
/// document does not yet exist, it will be created. If you pass [SetOptions],
/// the provided data will be merged into an existing document.
Future<void> setData(Map<String, dynamic> data, [SetOptions options]) {
/// Writes to the document referred to by this [DocumentReference].
///
/// If the document does not yet exist, it will be created.
///
/// If [merge] is true, the provided data will be merged into an
/// existing document instead of overwriting.
Future<void> setData(Map<String, dynamic> data, {bool merge: false}) {
return Firestore.channel.invokeMethod(
'DocumentReference#setData',
<String, dynamic>{
'app': firestore.app.name,
'path': path,
'data': data,
'options': options?._data,
'options': <String, bool>{'merge': merge},
},
);
}
Expand Down Expand Up @@ -95,7 +98,7 @@ class DocumentReference {

/// Notifies of documents at this location
// TODO(jackson): Reduce code duplication with [Query]
Stream<DocumentSnapshot> get snapshots {
Stream<DocumentSnapshot> snapshots() {
Future<int> _handle;
// It's fine to let the StreamController be garbage collected once all the
// subscribers have cancelled; this analyzer warning is safe to ignore.
Expand Down
2 changes: 1 addition & 1 deletion packages/cloud_firestore/lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Query {

/// Notifies of query results at this location
// TODO(jackson): Reduce code duplication with [DocumentReference]
Stream<QuerySnapshot> get snapshots {
Stream<QuerySnapshot> snapshots() {
Future<int> _handle;
// It's fine to let the StreamController be garbage collected once all the
// subscribers have cancelled; this analyzer warning is safe to ignore.
Expand Down
24 changes: 0 additions & 24 deletions packages/cloud_firestore/lib/src/set_options.dart

This file was deleted.

8 changes: 4 additions & 4 deletions packages/cloud_firestore/lib/src/write_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ class WriteBatch {
///
/// If the document does not yet exist, it will be created.
///
/// If you pass [SetOptions], the provided data will be merged into an
/// existing document.
/// If [merge] is true, the provided data will be merged into an
/// existing document instead of overwriting.
void setData(DocumentReference document, Map<String, dynamic> data,
[SetOptions options]) {
{bool merge: false}) {
if (!_committed) {
_handle.then((dynamic handle) {
_actions.add(
Expand All @@ -75,7 +75,7 @@ class WriteBatch {
'handle': handle,
'path': document.path,
'data': data,
'options': options?._data,
'options': <String, bool>{'merge': merge},
},
),
);
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.6.3
version: 0.7.0

flutter:
plugin:
Expand Down
20 changes: 9 additions & 11 deletions packages/cloud_firestore/test/cloud_firestore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void main() {
});
test('listen', () async {
final QuerySnapshot snapshot =
await collectionReference.snapshots.first;
await collectionReference.snapshots().first;
final DocumentSnapshot document = snapshot.documents[0];
expect(document.documentID, equals('0'));
expect(document.reference.path, equals('foo/0'));
Expand Down Expand Up @@ -275,7 +275,7 @@ void main() {
final StreamSubscription<QuerySnapshot> subscription =
collectionReference
.where('createdAt', isLessThan: 100)
.snapshots
.snapshots()
.listen((QuerySnapshot querySnapshot) {});
subscription.cancel();
await new Future<Null>.delayed(Duration.zero);
Expand Down Expand Up @@ -306,7 +306,7 @@ void main() {
final StreamSubscription<QuerySnapshot> subscription =
collectionReference
.where('profile', isNull: true)
.snapshots
.snapshots()
.listen((QuerySnapshot querySnapshot) {});
subscription.cancel();
await new Future<Null>.delayed(Duration.zero);
Expand Down Expand Up @@ -337,7 +337,7 @@ void main() {
final StreamSubscription<QuerySnapshot> subscription =
collectionReference
.orderBy('createdAt')
.snapshots
.snapshots()
.listen((QuerySnapshot querySnapshot) {});
subscription.cancel();
await new Future<Null>.delayed(Duration.zero);
Expand Down Expand Up @@ -369,7 +369,7 @@ void main() {
group('DocumentReference', () {
test('listen', () async {
final DocumentSnapshot snapshot =
await firestore.document('path/to/foo').snapshots.first;
await firestore.document('path/to/foo').snapshots().first;
expect(snapshot.documentID, equals('foo'));
expect(snapshot.reference.path, equals('path/to/foo'));
expect(snapshot.data, equals(kMockDocumentSnapshotData));
Expand Down Expand Up @@ -405,7 +405,7 @@ void main() {
'app': app.name,
'path': 'foo/bar',
'data': <String, String>{'bazKey': 'quxValue'},
'options': null,
'options': <String, bool>{'merge': false},
},
),
],
Expand All @@ -414,8 +414,7 @@ void main() {
test('merge set', () async {
await collectionReference
.document('bar')
.setData(<String, String>{'bazKey': 'quxValue'}, SetOptions.merge);
expect(SetOptions.merge, isNotNull);
.setData(<String, String>{'bazKey': 'quxValue'}, merge: true);
expect(
log,
<Matcher>[
Expand Down Expand Up @@ -576,7 +575,7 @@ void main() {
'handle': 1,
'path': 'foo/bar',
'data': <String, String>{'bazKey': 'quxValue'},
'options': null,
'options': <String, bool>{'merge': false},
},
),
isMethodCall(
Expand All @@ -593,10 +592,9 @@ void main() {
batch.setData(
collectionReference.document('bar'),
<String, String>{'bazKey': 'quxValue'},
SetOptions.merge,
merge: true,
);
await batch.commit();
expect(SetOptions.merge, isNotNull);
expect(
log,
<Matcher>[
Expand Down

0 comments on commit 4f7ec09

Please sign in to comment.