get method

Reads the query's data once, preferring the server.

  1. When the client already holds the query's data complete (server data kept for an active listener, or pending local writes that cover the query, with pending writes laid over it), it answers from that with no round trip.

  2. Otherwise it asks the server and waits, with no client timeout while connected. The answer also raises events on the listeners it completes, before this Future completes.

  3. When not connected, it waits up to 3 seconds for a connection, then answers from the persisted cache, or fails with unavailable. After FirebaseDatabase.goOffline it does not wait.

  4. When the connection drops while the request is out, case 3 applies from then.

  5. When the server refuses the read it fails with permission-denied, never with a cached answer.

  6. A .info path answers from the local state at once.

Other failures keep their code: network-error after two connections that each fell silent, index-not-defined for a filtered query the rules cannot serve, database-closed after FirebaseDatabase.close. Without persistence there is no persisted cache, so case 3 ends in unavailable.

Outside case 1 the answer does not include this client's pending writes.

A query with an order but no range and no limit asks the server for all the data at its path and orders the children here, as a listener does, so it needs no .indexOn rule. A query with a range or a limit is sent as it is and fails with index-not-defined when the rules have no .indexOn for its order.

Implementation

Future<DataSnapshot> get() {
  final DatabaseReference location = ref;
  return database.request<DataSnapshot>((
    Repo repo,
    void Function(DataSnapshot) complete,
    void Function(Object, [StackTrace?]) fail,
  ) {
    repo
        .getValue(spec)
        .then<void>(
          (Node node) =>
              complete(DataSnapshot.internal(location, node, spec.index)),
          onError: (Object error, StackTrace stackTrace) =>
              fail(toApiError(error), stackTrace),
        );
  });
}