Query

public class Query

Known direct subclasses
DatabaseReference

A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location.


The Query class (and its subclass, DatabaseReference) are used for reading data. Listeners are attached, and they will be triggered when the corresponding data changes. Instances of Query are obtained by calling startAt(), endAt(), or limit() on a DatabaseReference.

Summary

Public methods

@NonNull ChildEventListener

Add a listener for child events occurring at this location.

void

Add a listener for a single change in the data at this location.

@NonNull ValueEventListener

Add a listener for changes in the data at this location.

@NonNull Query

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query
endAt(double value)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query
endAt(boolean value)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

@NonNull Query
endAt(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

@NonNull Query
endAt(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

@NonNull Query

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

@NonNull Query
endBefore(double value)

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

@NonNull Query
endBefore(boolean value)

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

@NonNull Query

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

@NonNull Query
endBefore(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

@NonNull Query
endBefore(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

@NonNull Query

Creates a query constrained to only return child nodes with the given value.

@NonNull Query
equalTo(double value)

Creates a query constrained to only return child nodes with the given value.

@NonNull Query
equalTo(boolean value)

Creates a query constrained to only return child nodes with the given value.

@NonNull Query

Creates a query constrained to only return the child node with the given key and value.

@NonNull Query
equalTo(double value, @Nullable String key)

Creates a query constrained to only return the child node with the given key and value.

@NonNull Query
equalTo(boolean value, @Nullable String key)

Creates a query constrained to only return the child node with the given key and value.

@NonNull Task<DataSnapshot>
get()

Gets the server values for this query.

@NonNull DatabaseReference
void
keepSynced(boolean keepSynced)

By calling `keepSynced(true)` on a location, the data for that location will automatically be downloaded and kept in sync, even when no listeners are attached for that location.

@NonNull Query
limitToFirst(int limit)

Creates a query with limit and anchor it to the start of the window.

@NonNull Query
limitToLast(int limit)

Creates a query with limit and anchor it to the end of the window.

@NonNull Query

Creates a query in which child nodes are ordered by the values of the specified path.

@NonNull Query

Creates a query in which child nodes are ordered by their keys.

@NonNull Query

Creates a query in which child nodes are ordered by their priorities.

@NonNull Query

Creates a query in which nodes are ordered by their value

void

Remove the specified listener from this location.

void

Remove the specified listener from this location.

@NonNull Query

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

@NonNull Query
startAfter(double value)

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

@NonNull Query
startAfter(boolean value)

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

@NonNull Query

Creates a query constrained to only return child nodes with a value greater or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

@NonNull Query
startAfter(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

@NonNull Query
startAfter(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

@NonNull Query

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query
startAt(double value)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query
startAt(boolean value)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

@NonNull Query

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

@NonNull Query
startAt(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

@NonNull Query
startAt(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

Extension functions

final @NonNull Flow<@NonNull ChildEvent>
final @NonNull Flow<@NonNull ChildEvent>
final @NonNull Flow<@NonNull DataSnapshot>
final @NonNull Flow<@NonNull DataSnapshot>
final @NonNull Flow<T>
<T extends Object> DatabaseKt.values(@NonNull Query receiver)

Starts listening to this query and emits its values converted to a POJO via a Flow.

final @NonNull Flow<T>
<T extends Object> DatabaseKt.values(@NonNull Query receiver)

This method is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

Public methods

addChildEventListener

public @NonNull ChildEventListener addChildEventListener(@NonNull ChildEventListener listener)

Add a listener for child events occurring at this location. When child locations are added, removed, changed, or moved, the listener will be triggered for the appropriate event

Parameters
@NonNull ChildEventListener listener

The listener to be called with changes

Returns
@NonNull ChildEventListener

A reference to the listener provided. Save this to remove the listener later.

addListenerForSingleValueEvent

public void addListenerForSingleValueEvent(@NonNull ValueEventListener listener)

Add a listener for a single change in the data at this location. This listener will be triggered once with the value of the data at the location.

Parameters
@NonNull ValueEventListener listener

The listener to be called with the data

addValueEventListener

public @NonNull ValueEventListener addValueEventListener(@NonNull ValueEventListener listener)

Add a listener for changes in the data at this location. Each time the data changes, your listener will be called with an immutable snapshot of the data.

Parameters
@NonNull ValueEventListener listener

The listener to be called with changes

Returns
@NonNull ValueEventListener

A reference to the listener provided. Save this to remove the listener later.

endAt

public @NonNull Query endAt(@Nullable String value)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
@Nullable String value

The value to end at, inclusive

Returns
@NonNull Query

A query with the new constraint

endAt

public @NonNull Query endAt(double value)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
double value

The value to end at, inclusive

Returns
@NonNull Query

A query with the new constraint

endAt

public @NonNull Query endAt(boolean value)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default.

2.0

Parameters
boolean value

The value to end at, inclusive

Returns
@NonNull Query

A query with the new constraint

endAt

public @NonNull Query endAt(@Nullable String value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

Parameters
@Nullable String value

The value to end at, inclusive

@Nullable String key

The key to end at, inclusive

Returns
@NonNull Query

A query with the new constraint

endAt

public @NonNull Query endAt(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

Parameters
double value

The value to end at, inclusive

@Nullable String key

The key to end at, inclusive

Returns
@NonNull Query

A query with the new constraint

endAt

public @NonNull Query endAt(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than or equal to the given key.

2.0

Parameters
boolean value

The value to end at, inclusive

@Nullable String key

The key to end at, inclusive

Returns
@NonNull Query

A query with the new constraint

endBefore

public @NonNull Query endBefore(@Nullable String value)

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

19.6

Parameters
@Nullable String value

The value to end at, exclusive

Returns
@NonNull Query

A query with the new constraint

endBefore

public @NonNull Query endBefore(double value)

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

19.6

Parameters
double value

The value to end at, exclusive

Returns
@NonNull Query

A query with the new constraint

endBefore

public @NonNull Query endBefore(boolean value)

Creates a query constrained to only return child nodes with a value less than the given value, using the given orderBy directive or priority as default.

19.6

Parameters
boolean value

The value to end at, exclusive

Returns
@NonNull Query

A query with the new constraint

endBefore

public @NonNull Query endBefore(@Nullable String value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

19.6

Parameters
@Nullable String value

The value to end at

@Nullable String key

The key to end at, exclusive

Returns
@NonNull Query

A query with the new constraint

endBefore

public @NonNull Query endBefore(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

19.6

Parameters
double value

The value to end at

@Nullable String key

The key to end at, exclusive

Returns
@NonNull Query

A query with the new constraint

endBefore

public @NonNull Query endBefore(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value less than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key less than the given key.

19.6

Parameters
boolean value

The value to end at

@Nullable String key

The key to end at, exclusive

Returns
@NonNull Query

A query with the new constraint

equalTo

public @NonNull Query equalTo(@Nullable String value)

Creates a query constrained to only return child nodes with the given value.

Parameters
@Nullable String value

The value to query for

Returns
@NonNull Query

A query with the new constraint

equalTo

public @NonNull Query equalTo(double value)

Creates a query constrained to only return child nodes with the given value.

Parameters
double value

The value to query for

Returns
@NonNull Query

A query with the new constraint

equalTo

public @NonNull Query equalTo(boolean value)

Creates a query constrained to only return child nodes with the given value.

2.0

Parameters
boolean value

The value to query for

Returns
@NonNull Query

A query with the new constraint

equalTo

public @NonNull Query equalTo(@Nullable String value, @Nullable String key)

Creates a query constrained to only return the child node with the given key and value. Note that there is at most one such child as names are unique.

Parameters
@Nullable String value

The value to query for

@Nullable String key

The key of the child

Returns
@NonNull Query

A query with the new constraint

equalTo

public @NonNull Query equalTo(double value, @Nullable String key)

Creates a query constrained to only return the child node with the given key and value. Note that there is at most one such child as keys are unique.

Parameters
double value

The value to query for

@Nullable String key

The key of the child

Returns
@NonNull Query

A query with the new constraint

equalTo

public @NonNull Query equalTo(boolean value, @Nullable String key)

Creates a query constrained to only return the child node with the given key and value. Note that there is at most one such child as keys are unique.

Parameters
boolean value

The value to query for

@Nullable String key

The name of the child

Returns
@NonNull Query

A query with the new constraint

get

public @NonNull Task<DataSnapshotget()

Gets the server values for this query. Updates the cache and raises events if successful. If not connected, falls back to a locally-cached value.

getRef

public @NonNull DatabaseReference getRef()
Returns
@NonNull DatabaseReference

A DatabaseReference to this location

keepSynced

public void keepSynced(boolean keepSynced)

By calling `keepSynced(true)` on a location, the data for that location will automatically be downloaded and kept in sync, even when no listeners are attached for that location. Additionally, while a location is kept synced, it will not be evicted from the persistent disk cache.

2.3

Parameters
boolean keepSynced

Pass `true` to keep this location synchronized, pass `false` to stop synchronization.

limitToFirst

public @NonNull Query limitToFirst(int limit)

Creates a query with limit and anchor it to the start of the window.

2.0

Parameters
int limit

The maximum number of child nodes to return

Returns
@NonNull Query

A query with the new constraint

limitToLast

public @NonNull Query limitToLast(int limit)

Creates a query with limit and anchor it to the end of the window.

2.0

Parameters
int limit

The maximum number of child nodes to return

Returns
@NonNull Query

A query with the new constraint

orderByChild

public @NonNull Query orderByChild(@NonNull String path)

Creates a query in which child nodes are ordered by the values of the specified path.

2.0

Parameters
@NonNull String path

The path to the child node to use for sorting

Returns
@NonNull Query

A query with the new constraint

orderByKey

public @NonNull Query orderByKey()

Creates a query in which child nodes are ordered by their keys.

2.0

Returns
@NonNull Query

A query with the new constraint

orderByPriority

public @NonNull Query orderByPriority()

Creates a query in which child nodes are ordered by their priorities.

2.0

Returns
@NonNull Query

A query with the new constraint

orderByValue

public @NonNull Query orderByValue()

Creates a query in which nodes are ordered by their value

2.2

Returns
@NonNull Query

A query with the new constraint

removeEventListener

public void removeEventListener(@NonNull ChildEventListener listener)

Remove the specified listener from this location.

Parameters
@NonNull ChildEventListener listener

The listener to remove

removeEventListener

public void removeEventListener(@NonNull ValueEventListener listener)

Remove the specified listener from this location.

Parameters
@NonNull ValueEventListener listener

The listener to remove

startAfter

public @NonNull Query startAfter(@Nullable String value)

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

19.6

Parameters
@Nullable String value

The value to start at, exclusive

Returns
@NonNull Query

A query with the new constraint

startAfter

public @NonNull Query startAfter(double value)

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

19.6

Parameters
double value

The value to start at, exclusive

Returns
@NonNull Query

A query with the new constraint

startAfter

public @NonNull Query startAfter(boolean value)

Creates a query constrained to only return child nodes with a value greater than the given value, using the given orderBy directive or priority as default.

19.6

Parameters
boolean value

The value to start at, exclusive

Returns
@NonNull Query

A query with the new constraint

startAfter

public @NonNull Query startAfter(@Nullable String value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

19.6

Parameters
@Nullable String value

The value to start at

@Nullable String key

The key to start at, exclusive

Returns
@NonNull Query

A query with the new constraint

startAfter

public @NonNull Query startAfter(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

19.6

Parameters
double value

The value to start at

@Nullable String key

The key name to start at, exclusive

Returns
@NonNull Query

A query with the new constraint

startAfter

public @NonNull Query startAfter(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than the given key.

19.6

Parameters
boolean value

The value to start at

@Nullable String key

The key to start at, exclusive

Returns
@NonNull Query

A query with the new constraint

startAt

public @NonNull Query startAt(@Nullable String value)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
@Nullable String value

The value to start at, inclusive

Returns
@NonNull Query

A query with the new constraint

startAt

public @NonNull Query startAt(double value)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

Parameters
double value

The value to start at, inclusive

Returns
@NonNull Query

A query with the new constraint

startAt

public @NonNull Query startAt(boolean value)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default.

2.0

Parameters
boolean value

The value to start at, inclusive

Returns
@NonNull Query

A query with the new constraint

startAt

public @NonNull Query startAt(@Nullable String value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

Parameters
@Nullable String value

The priority to start at, inclusive

@Nullable String key

The key to start at, inclusive

Returns
@NonNull Query

A query with the new constraint

startAt

public @NonNull Query startAt(double value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

Parameters
double value

The priority to start at, inclusive

@Nullable String key

The key name to start at, inclusive

Returns
@NonNull Query

A query with the new constraint

startAt

public @NonNull Query startAt(boolean value, @Nullable String key)

Creates a query constrained to only return child nodes with a value greater than or equal to the given value, using the given orderBy directive or priority as default, and additionally only child nodes with a key greater than or equal to the given key.

2.0

Parameters
boolean value

The priority to start at, inclusive

@Nullable String key

The key to start at, inclusive

Returns
@NonNull Query

A query with the new constraint

Extension functions

DatabaseKt.getChildEvents

public final @NonNull Flow<@NonNull ChildEventDatabaseKt.getChildEvents(@NonNull Query receiver)

DatabaseKt.getChildEvents

public final @NonNull Flow<@NonNull ChildEventDatabaseKt.getChildEvents(@NonNull Query receiver)

DatabaseKt.getSnapshots

public final @NonNull Flow<@NonNull DataSnapshotDatabaseKt.getSnapshots(@NonNull Query receiver)

DatabaseKt.getSnapshots

public final @NonNull Flow<@NonNull DataSnapshotDatabaseKt.getSnapshots(@NonNull Query receiver)

DatabaseKt.values

public final @NonNull Flow<T> <T extends Object> DatabaseKt.values(@NonNull Query receiver)

Starts listening to this query and emits its values converted to a POJO via a Flow.

  • When the returned flow starts being collected, a ValueEventListener will be attached.

  • When the flow completes, the listener will be removed.

DatabaseKt.values

public final @NonNull Flow<T> <T extends Object> DatabaseKt.values(@NonNull Query receiver)

Starts listening to this query and emits its values converted to a POJO via a Flow.

  • When the returned flow starts being collected, a ValueEventListener will be attached.

  • When the flow completes, the listener will be removed.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-database-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.