Query class base

A location with an optional order, range and limit. Immutable: every modifier returns a new query.

final Query top = db.ref('scores').orderByValue().limitToLast(10);
top.onValue.listen((DatabaseEvent e) {
  for (final DataSnapshot s in e.snapshot.children) {
    print('${s.key}: ${s.value}');
  }
});

Order. Bounds and limits apply to the query's order. With no orderBy* that order is the priority order, not the value: ref.startAt(2) keeps the children whose priority is at least 2, so on data written without priorities it keeps none. Call an orderBy* first.

Validation. The client checks a query when you build it, in release builds too: one orderBy*; at most one start bound and one end bound; equalTo excludes both; a key: argument only where the order allows it; a bound of a type the order allows (a String for orderByKey; null, a number or a String for the priority order; no non-empty map or list for any order); a limit above 0. A bound is checked against the order in effect when it is set, and again by a later orderBy*. A violation throws an ArgumentError (a bad argument) or a StateError (a second orderBy*, start, end or limit) at the call; a second bound of a refused type throws the ArgumentError.

Streams. Every event getter returns a stream made with Stream.multi: not broadcast, and each listen, also a second listen on the same stream object, is its own registration that gets its own initial events. Equal queries share one listen on the server. Cancelling a subscription removes it at once, without waiting for the server; the last one on a query removes the server listen. A stream ends only when the database closes or its app is deleted (done, with no error event), when the server cancels the listen (error, for example permission-denied, then done), or when the database could not start (that error, then done). A lost connection is never a stream error.

Local writes. A write of this client raises events before the server answers; when the server refuses it, the listeners get events again with the data restored. A transaction with applyLocally: false raises none before the answer.

Delivery. No listener runs inside the call that caused it: events go through one queue per database, drained in a microtask. FirebaseDatabase.close drops the events still queued for open streams, then each gets done in a microtask; a paused subscription gets done after resume, following the events already buffered in it. Inside one change, events come child removed, added, moved, changed, then value; deeper locations before shallower ones. Changes are delivered in the order they were applied. Pausing a subscription buffers its events without bound; nothing is dropped. Order across subscriptions holds only for listen callbacks that are not paused: await for pauses after each event, so it loses order relative to other streams (use onChildEvents to keep the order of child events). An exception thrown by a listener goes to that listener's zone; its subscription stays open.

Cache. A new listener on a location whose data is held complete (by another listener, or by a pending local write) gets its initial events from that data with no round trip. Two queries are equal when they have the same database, path and query parameters.

Properties

database → FirebaseDatabase
The database this query reads.
final
hashCode → int
The hash code for this object.
no setteroverride
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 setter
onChildChanged → Stream<DatabaseEvent>
A DatabaseEventType.childChanged event each time a child's data changes.
no setter
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 setter
onChildMoved → Stream<DatabaseEvent>
A DatabaseEventType.childMoved event each time a child changes place in the order.
no setter
onChildRemoved → Stream<DatabaseEvent>
A DatabaseEventType.childRemoved event each time a child leaves the results.
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 exists false. 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 the value map.
no setter
path → String
The path of the location: '/a/b', or '/' at the root.
no setter
ref → DatabaseReference
The reference to this query's location, without order, range or limit.
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 setter

Methods

endAt(Object? value, {String? key}) → Query
Ends the range at value, included (and at key among children equal to value). A null value is a real bound. Throws a StateError when an end bound is already set, and an ArgumentError when key or the type of value is not allowed by the order.
endBefore(Object? value, {String? key}) → Query
Ends the range just before value (and before key among children equal to value). A null value is a real bound. Throws as endAt does.
equalTo(Object? value, {String? key}) → Query
Keeps only the children equal to value (and with key key when given): a start and an end bound at once, so it throws when either is already set.
get() → Future<DataSnapshot>
Reads the query's data once, preferring the server.
limitToFirst(int limit) → Query
Keeps the first limit children in the order; limit must be above 0 and no limit may be set yet.
limitToLast(int limit) → Query
Keeps the last limit children in the order; limit must be above 0 and no limit may be set yet.
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 type and stops listening.
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.
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.
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*.
orderByValue() → Query
Orders by the children's values: null, then false, true, numbers, strings, then objects by key.
startAfter(Object? value, {String? key}) → Query
Starts the range just after value (and after key among children equal to value). A null value is a real bound. Throws as startAt does.
startAt(Object? value, {String? key}) → Query
Starts the range at value, included, and at children whose key is at least key among those equal to value. A null value is a real bound (null priority or value). With no orderBy*, value is a priority. Throws a StateError when a start bound is already set, and an ArgumentError when key or the type of value is not allowed by the order (see the class documentation).
startKeepingSynced() → Future<void>
Keeps this query's data synchronised even with no listener, until stopKeepingSynced.
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.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
override