Skip to content

Commit 3c97480

Browse files
fix(database): DataSnapshot is always present (firebase#6754)
* fix(database): DataSnapshot is always present * feat(database): DataSnapshot.exists * test(database): DataSnapshot.exists tests * format: run formatter * test(database): update unit test mock
1 parent ffec135 commit 3c97480

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

packages/firebase_database/firebase_database/example/test_driver/firebase_database_e2e.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,28 @@ void testsMain() {
100100
.child('ordered/one')
101101
.get();
102102
expect(dataSnapshot, isNot(null));
103-
expect(dataSnapshot!.key, 'one');
103+
expect(dataSnapshot.key, 'one');
104104
expect(dataSnapshot.value['ref'], 'one');
105105
expect(dataSnapshot.value['value'], 23);
106106
});
107+
108+
test('DataSnapshot.exists is false for no data', () async {
109+
final dataSnapshot = await FirebaseDatabase.instance
110+
.reference()
111+
.child('a-non-existing-reference')
112+
.get();
113+
114+
expect(dataSnapshot.exists, false);
115+
});
116+
117+
test('DataSnapshot.exists is true for existing data', () async {
118+
final dataSnapshot = await FirebaseDatabase.instance
119+
.reference()
120+
.child('ordered/one')
121+
.get();
122+
123+
expect(dataSnapshot.exists, true);
124+
});
107125
});
108126
}
109127

packages/firebase_database/firebase_database/lib/src/event.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Event {
2929
/// A DataSnapshot contains data from a Firebase Database location.
3030
/// Any time you read Firebase data, you receive the data as a DataSnapshot.
3131
class DataSnapshot {
32-
DataSnapshot._(this.key, this.value);
32+
DataSnapshot._(this.key, this.value) : exists = value != null;
3333

3434
factory DataSnapshot._fromJson(
3535
Map<Object?, Object?> _data,
@@ -55,6 +55,9 @@ class DataSnapshot {
5555

5656
/// Returns the contents of this data snapshot as native types.
5757
final dynamic value;
58+
59+
/// Ascertains whether the value exists at the Firebase Database location.
60+
final bool exists;
5861
}
5962

6063
class MutableData {

packages/firebase_database/firebase_database/lib/src/query.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Query {
8282
Future<DataSnapshot> once() async => (await onValue.first).snapshot;
8383

8484
/// Gets the most up-to-date result for this query.
85-
Future<DataSnapshot?> get() async {
85+
Future<DataSnapshot> get() async {
8686
final result = await _database._channel.invokeMethod<Map<dynamic, dynamic>>(
8787
'Query#get',
8888
<String, dynamic>{

packages/firebase_database/firebase_database/test/firebase_list_test.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,17 @@ class MockEvent implements Event {
330330
}
331331

332332
class MockDataSnapshot implements DataSnapshot {
333-
MockDataSnapshot(this.key, this.value);
333+
MockDataSnapshot(this.key, this.value) : exists = value != null;
334334

335335
@override
336336
final String key;
337337

338338
@override
339339
final dynamic value;
340340

341+
@override
342+
final bool exists;
343+
341344
@override
342345
// ignore: no_runtimetype_tostring
343346
String toString() => '$runtimeType[$key, $value]';

0 commit comments

Comments
 (0)