DatabaseReference class final
A location in the database. Creating one makes no request.
Get one from FirebaseDatabase.ref, and walk with child, parent and root. As a Query it reads and listens; it also writes.
Writes. A write's local events are queued before its Future completes, so listeners see it at once,
offline too. The Future completes when the server answers; while offline it stays pending, not failed. A
refused write rolls back with events, then fails (for example permission-denied). Writes are at least
once: a write whose acknowledgement was lost is sent again after reconnecting. After
FirebaseDatabase.close a valid write fails its Future with database-closed; an invalid value still
throws at the call.
Values. A value is null, a bool, a num, a String, a List or a Map with String keys, at any
depth, or a ServerValue. A map may also hold .priority, and .value when it has no other child: a
value with its priority. A .priority key wins over a priority argument. The client checks the value
at the call and throws an ArgumentError for any other key with forbidden
characters (., #, $, [, ], / or control characters), a path deeper than 32 segments or
longer than 768 bytes (this location's path and the keys inside the value, in UTF-8, with one byte for
each /), NaN or Infinity, a priority argument that is not null, a num, a String or a
ServerValue, a leaf over 10 MB, a map key that is not a String, or a lone surrogate in a string or
key. child, push, FirebaseDatabase.ref, setPriority and an empty update check no depth or
length. A List is written as a map with index keys. An empty map or list, or one that holds only
nulls, is no data: writing it removes the location. A -0.0 is sent as 0, and a double with no
fraction is stored and read back as an int (5.0 reads as 5). Writes under .info throw an
ArgumentError.
final DatabaseReference post = db.ref('posts').push();
await post.set(<String, Object?>{'title': 'Hello', 'at': ServerValue.timestamp});
await post.update(<String, Object?>{'stats/views': 0});
Properties
- database → FirebaseDatabase
-
The database this query reads.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → String?
-
The last segment of the path, or null at the root.
no setter
-
onChildAdded
→ Stream<
DatabaseEvent> -
A DatabaseEventType.childAdded event per child, first for each existing child, then for each new
one. An empty location gives no event until a child appears.
no setterinherited
-
onChildChanged
→ Stream<
DatabaseEvent> -
A DatabaseEventType.childChanged event each time a child's data changes.
no setterinherited
-
onChildEvents
→ Stream<
DatabaseEvent> -
The four child event kinds in one stream, in their true order, with no value events and no batching
per change; switch on DatabaseEvent.type. Unlike four separate streams, it keeps the order of child
events under pause and
await for.no setterinherited -
onChildMoved
→ Stream<
DatabaseEvent> -
A DatabaseEventType.childMoved event each time a child changes place in the order.
no setterinherited
-
onChildRemoved
→ Stream<
DatabaseEvent> -
A DatabaseEventType.childRemoved event each time a child leaves the results.
no setterinherited
- onDisconnect → OnDisconnect
-
The writes the server applies at this location when this client disconnects.
no setter
-
onValue
→ Stream<
DatabaseEvent> -
The value of the query: its current data first, then again after every change. An empty
location gives a first event whose snapshot has
existsfalse. While offline the first event comes from data the client already holds, else it waits for the server; the loss of the connection is not an error of the stream (see FirebaseDatabase.onConnected). Read the children in the query's order from DataSnapshot.children, not from thevaluemap.no setterinherited - parent → DatabaseReference?
-
The reference one level up, or null at the root.
no setter
- path → String
-
The path of the location:
'/a/b', or'/'at the root.no setterinherited - ref → DatabaseReference
-
The reference to this query's location, without order, range or limit.
no setterinherited
- root → DatabaseReference
-
The root of the database.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- url → Uri
-
The scheme, host and percent-encoded path of this location, for example
https://my-project-default-rtdb.firebaseio.com/a/b. Unlike FirebaseDatabase.databaseUrl it never has the?ns=query of an emulator.no setterinherited
Methods
-
child(
String path) → DatabaseReference -
The reference at
pathbelow this one.pathmay have several segments; empty segments are dropped, sochild('a//b')ischild('a/b')andchild('/')is this location. Throws an ArgumentError for an emptypathor an invalid key;.infois accepted on the root only. It checks no depth or length; see the class documentation for the writes that do. -
endAt(
Object? value, {String? key}) → Query -
Ends the range at
value, included (and atkeyamong children equal tovalue). Anullvalueis a real bound. Throws a StateError when an end bound is already set, and an ArgumentError whenkeyor the type ofvalueis not allowed by the order.inherited -
endBefore(
Object? value, {String? key}) → Query -
Ends the range just before
value(and beforekeyamong children equal tovalue). Anullvalueis a real bound. Throws as endAt does.inherited -
equalTo(
Object? value, {String? key}) → Query -
Keeps only the children equal to
value(and with keykeywhen given): a start and an end bound at once, so it throws when either is already set.inherited -
get(
) → Future< DataSnapshot> -
Reads the query's data once, preferring the server.
inherited
-
limitToFirst(
int limit) → Query -
Keeps the first
limitchildren in the order;limitmust be above 0 and no limit may be set yet.inherited -
limitToLast(
int limit) → Query -
Keeps the last
limitchildren in the order;limitmust be above 0 and no limit may be set yet.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
once(
[DatabaseEventType type = DatabaseEventType.value]) → Future< DatabaseEvent> -
Completes with the first event of
typeand stops listening.inherited -
orderByChild(
String path) → Query -
Orders by the value of the child at
path(which may have several segments) of each child. Children without it come first.inherited -
orderByKey(
) → Query -
Orders by child key: keys that parse as 32-bit integers (leading zeros allowed) come first, by value,
ties going to the shorter string; then all other keys by UTF-16 code units.
inherited
-
orderByPriority(
) → Query -
Orders by the children's priorities: children without one first, then numbers, then strings;
ties go by key. This is also the order of a query with no
orderBy*.inherited -
orderByValue(
) → Query -
Orders by the children's values: null, then false, true, numbers, strings, then objects by key.
inherited
-
push(
) → DatabaseReference -
A new child with a unique key made on this device, ordered by creation time. It only makes the
reference: nothing is written until you call set on it. Throws an ArgumentError under
.info. -
remove(
) → Future< void> -
Removes the data at this location: the same as
set(null). -
runTransaction(
TransactionHandler handler, {bool applyLocally = true}) → Future< TransactionResult> -
Atomically changes the data at this location with
handler. -
set(
Object? value, {Object? priority}) → Future< void> -
Replaces the data at this location with
value, and its priority withpriority. Withoutprioritythe location has no priority afterwards, unlessvalueis a map with a.prioritykey. Anullvalue, or an empty map or list (see the class documentation), removes the data, and no priority is kept. Fires local events before the Future completes; see the class documentation for completion, rollback and value rules. A transaction at, above or below this location may fail withoverridden-by-set(see runTransaction). Withpersistencethe write is stored on disk and sent again after a restart; the Future of the run that made it is gone then. -
setPriority(
Object? priority) → Future< void> -
Sets the priority of this location: null, a
num, aStringor aServerValue. The client keeps no priority for a location that holds no data: there the write is sent but raises no local event. To create data with a priority, useset(value, priority: p). It checks no depth or length. Withpersistencethe write is stored on disk and sent again after a restart; the Future of the run that made it is gone then. -
startAfter(
Object? value, {String? key}) → Query -
Starts the range just after
value(and afterkeyamong children equal tovalue). Anullvalueis a real bound. Throws as startAt does.inherited -
startAt(
Object? value, {String? key}) → Query -
Starts the range at
value, included, and at children whose key is at leastkeyamong those equal tovalue. Anullvalueis a real bound (null priority or value). With noorderBy*,valueis a priority. Throws a StateError when a start bound is already set, and an ArgumentError whenkeyor the type ofvalueis not allowed by the order (see the class documentation).inherited -
startKeepingSynced(
) → Future< void> -
Keeps this query's data synchronised even with no listener, until stopKeepingSynced.
inherited
-
stopKeepingSynced(
) → Future< void> -
Stops what startKeepingSynced started: the query's data is kept only while it has a
listener. Completes, fails and throws as startKeepingSynced does.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
update(
Map< String, Object?> values) → Future<void> -
Writes each entry of
valuesbelow this location, leaving other children alone. Keys may be multi-segment paths ('a/b'), which makes one atomic write to several places; anullvalue removes that child. Two keys where one path is the other or an ancestor of it ('a'and'a/b', or'a/b'and'/a//b') throw an ArgumentError;'a'and'ab'do not. A key that names this location itself ('','/'or any string of only slashes) throws an ArgumentError that names the key, and nothing is written; use set to replace the location or remove to delete it.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited