File tree Expand file tree Collapse file tree 4 files changed +28
-4
lines changed
packages/firebase_database/firebase_database Expand file tree Collapse file tree 4 files changed +28
-4
lines changed Original file line number Diff line number Diff line change @@ -100,10 +100,28 @@ void testsMain() {
100
100
.child ('ordered/one' )
101
101
.get ();
102
102
expect (dataSnapshot, isNot (null ));
103
- expect (dataSnapshot! .key, 'one' );
103
+ expect (dataSnapshot.key, 'one' );
104
104
expect (dataSnapshot.value['ref' ], 'one' );
105
105
expect (dataSnapshot.value['value' ], 23 );
106
106
});
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
+ });
107
125
});
108
126
}
109
127
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ class Event {
29
29
/// A DataSnapshot contains data from a Firebase Database location.
30
30
/// Any time you read Firebase data, you receive the data as a DataSnapshot.
31
31
class DataSnapshot {
32
- DataSnapshot ._(this .key, this .value);
32
+ DataSnapshot ._(this .key, this .value) : exists = value != null ;
33
33
34
34
factory DataSnapshot ._fromJson (
35
35
Map <Object ?, Object ?> _data,
@@ -55,6 +55,9 @@ class DataSnapshot {
55
55
56
56
/// Returns the contents of this data snapshot as native types.
57
57
final dynamic value;
58
+
59
+ /// Ascertains whether the value exists at the Firebase Database location.
60
+ final bool exists;
58
61
}
59
62
60
63
class MutableData {
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ class Query {
82
82
Future <DataSnapshot > once () async => (await onValue.first).snapshot;
83
83
84
84
/// Gets the most up-to-date result for this query.
85
- Future <DataSnapshot ? > get () async {
85
+ Future <DataSnapshot > get () async {
86
86
final result = await _database._channel.invokeMethod <Map <dynamic , dynamic >>(
87
87
'Query#get' ,
88
88
< String , dynamic > {
Original file line number Diff line number Diff line change @@ -330,14 +330,17 @@ class MockEvent implements Event {
330
330
}
331
331
332
332
class MockDataSnapshot implements DataSnapshot {
333
- MockDataSnapshot (this .key, this .value);
333
+ MockDataSnapshot (this .key, this .value) : exists = value != null ;
334
334
335
335
@override
336
336
final String key;
337
337
338
338
@override
339
339
final dynamic value;
340
340
341
+ @override
342
+ final bool exists;
343
+
341
344
@override
342
345
// ignore: no_runtimetype_tostring
343
346
String toString () => '$runtimeType [$key , $value ]' ;
You can’t perform that action at this time.
0 commit comments