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

@NonNullChildEventListener

Add a listener for child events occurring at this location.

void

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

@NonNullValueEventListener

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

@NonNullQuery

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.

@NonNullQuery
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.

@NonNullQuery
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.

@NonNullQuery

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.

@NonNullQuery
endAt(double value, @NullableString 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.

@NonNullQuery
endAt(boolean value, @NullableString 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.

@NonNullQuery

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.

@NonNullQuery
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.

@NonNullQuery
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.

@NonNullQuery

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.

@NonNullQuery
endBefore(double value, @NullableString 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.

@NonNullQuery
endBefore(boolean value, @NullableString 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.

@NonNullQuery

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

@NonNullQuery
equalTo(double value)

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

@NonNullQuery
equalTo(boolean value)

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

@NonNullQuery

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

@NonNullQuery
equalTo(double value, @NullableString key)

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

@NonNullQuery
equalTo(boolean value, @NullableString key)

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

@NonNullTask<DataSnapshot>
get()

Gets the server values for this query.

@NonNullDatabaseReference
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.

@NonNullQuery
limitToFirst(int limit)

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

@NonNullQuery
limitToLast(int limit)

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

@NonNullQuery

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

@NonNullQuery

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

@NonNullQuery

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

@NonNullQuery

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.

@NonNullQuery

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.

@NonNullQuery
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.

@NonNullQuery
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.

@NonNullQuery

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.

@NonNullQuery
startAfter(double value, @NullableString 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.

@NonNullQuery
startAfter(boolean value, @NullableString 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.

@NonNullQuery

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.

@NonNullQuery
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.

@NonNullQuery
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.

@NonNullQuery

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.

@NonNullQuery
startAt(double value, @NullableString 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.

@NonNullQuery
startAt(boolean value, @NullableString 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 @NonNullFlow<@NonNullChildEvent>
final @NonNullFlow<@NonNullChildEvent>
final @NonNullFlow<@NonNullDataSnapshot>
final @NonNullFlow<@NonNullDataSnapshot>
final @NonNullFlow<T>
<T extends Object> DatabaseKt.values(@NonNullQuery receiver)

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

final @NonNullFlow<T>
<T extends Object> DatabaseKt.values(@NonNullQuery 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 @NonNullChildEventListener addChildEventListener(@NonNullChildEventListener 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
@NonNullChildEventListener listener

The listener to be called with changes

Returns
@NonNullChildEventListener

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

addListenerForSingleValueEvent

public void addListenerForSingleValueEvent(@NonNullValueEventListener 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
@NonNullValueEventListener listener

The listener to be called with the data

addValueEventListener

public @NonNullValueEventListener addValueEventListener(@NonNullValueEventListener 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
@NonNullValueEventListener listener

The listener to be called with changes

Returns
@NonNullValueEventListener

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

endAt

public @NonNullQuery endAt(@NullableString 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
@NullableString value

The value to end at, inclusive

Returns
@NonNullQuery

A query with the new constraint

endAt

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

endAt

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

endAt

public @NonNullQuery endAt(@NullableString value, @NullableString 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
@NullableString value

The value to end at, inclusive

@NullableString key

The key to end at, inclusive

Returns
@NonNullQuery

A query with the new constraint

endAt

public @NonNullQuery endAt(double value, @NullableString 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

@NullableString key

The key to end at, inclusive

Returns
@NonNullQuery

A query with the new constraint

endAt

public @NonNullQuery endAt(boolean value, @NullableString 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

@NullableString key

The key to end at, inclusive

Returns
@NonNullQuery

A query with the new constraint

endBefore

public @NonNullQuery endBefore(@NullableString 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
@NullableString value

The value to end at, exclusive

Returns
@NonNullQuery

A query with the new constraint

endBefore

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

endBefore

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

endBefore

public @NonNullQuery endBefore(@NullableString value, @NullableString 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
@NullableString value

The value to end at

@NullableString key

The key to end at, exclusive

Returns
@NonNullQuery

A query with the new constraint

endBefore

public @NonNullQuery endBefore(double value, @NullableString 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

@NullableString key

The key to end at, exclusive

Returns
@NonNullQuery

A query with the new constraint

endBefore

public @NonNullQuery endBefore(boolean value, @NullableString 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

@NullableString key

The key to end at, exclusive

Returns
@NonNullQuery

A query with the new constraint

equalTo

public @NonNullQuery equalTo(@NullableString value)

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

Parameters
@NullableString value

The value to query for

Returns
@NonNullQuery

A query with the new constraint

equalTo

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

equalTo

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

equalTo

public @NonNullQuery equalTo(@NullableString value, @NullableString 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
@NullableString value

The value to query for

@NullableString key

The key of the child

Returns
@NonNullQuery

A query with the new constraint

equalTo

public @NonNullQuery equalTo(double value, @NullableString 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

@NullableString key

The key of the child

Returns
@NonNullQuery

A query with the new constraint

equalTo

public @NonNullQuery equalTo(boolean value, @NullableString 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

@NullableString key

The name of the child

Returns
@NonNullQuery

A query with the new constraint

get

public @NonNullTask<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 @NonNullDatabaseReference getRef()
Returns
@NonNullDatabaseReference

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 @NonNullQuery 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
@NonNullQuery

A query with the new constraint

limitToLast

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

orderByChild

public @NonNullQuery orderByChild(@NonNullString path)

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

2.0

Parameters
@NonNullString path

The path to the child node to use for sorting

Returns
@NonNullQuery

A query with the new constraint

orderByKey

public @NonNullQuery orderByKey()

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

2.0

Returns
@NonNullQuery

A query with the new constraint

orderByPriority

public @NonNullQuery orderByPriority()

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

2.0

Returns
@NonNullQuery

A query with the new constraint

orderByValue

public @NonNullQuery orderByValue()

Creates a query in which nodes are ordered by their value

2.2

Returns
@NonNullQuery

A query with the new constraint

removeEventListener

public void removeEventListener(@NonNullChildEventListener listener)

Remove the specified listener from this location.

Parameters
@NonNullChildEventListener listener

The listener to remove

removeEventListener

public void removeEventListener(@NonNullValueEventListener listener)

Remove the specified listener from this location.

Parameters
@NonNullValueEventListener listener

The listener to remove

startAfter

public @NonNullQuery startAfter(@NullableString 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
@NullableString value

The value to start at, exclusive

Returns
@NonNullQuery

A query with the new constraint

startAfter

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

startAfter

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

startAfter

public @NonNullQuery startAfter(@NullableString value, @NullableString 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
@NullableString value

The value to start at

@NullableString key

The key to start at, exclusive

Returns
@NonNullQuery

A query with the new constraint

startAfter

public @NonNullQuery startAfter(double value, @NullableString 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

@NullableString key

The key name to start at, exclusive

Returns
@NonNullQuery

A query with the new constraint

startAfter

public @NonNullQuery startAfter(boolean value, @NullableString 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

@NullableString key

The key to start at, exclusive

Returns
@NonNullQuery

A query with the new constraint

startAt

public @NonNullQuery startAt(@NullableString 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
@NullableString value

The value to start at, inclusive

Returns
@NonNullQuery

A query with the new constraint

startAt

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

startAt

public @NonNullQuery 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
@NonNullQuery

A query with the new constraint

startAt

public @NonNullQuery startAt(@NullableString value, @NullableString 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
@NullableString value

The priority to start at, inclusive

@NullableString key

The key to start at, inclusive

Returns
@NonNullQuery

A query with the new constraint

startAt

public @NonNullQuery startAt(double value, @NullableString 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

@NullableString key

The key name to start at, inclusive

Returns
@NonNullQuery

A query with the new constraint

startAt

public @NonNullQuery startAt(boolean value, @NullableString 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

@NullableString key

The key to start at, inclusive

Returns
@NonNullQuery

A query with the new constraint

Extension functions

DatabaseKt.getChildEvents

public final @NonNullFlow<@NonNullChildEventDatabaseKt.getChildEvents(@NonNullQuery receiver)

DatabaseKt.getChildEvents

public final @NonNullFlow<@NonNullChildEventDatabaseKt.getChildEvents(@NonNullQuery receiver)

DatabaseKt.getSnapshots

public final @NonNullFlow<@NonNullDataSnapshotDatabaseKt.getSnapshots(@NonNullQuery receiver)

DatabaseKt.getSnapshots

public final @NonNullFlow<@NonNullDataSnapshotDatabaseKt.getSnapshots(@NonNullQuery receiver)

DatabaseKt.values

public final @NonNullFlow<T> <T extends Object> DatabaseKt.values(@NonNullQuery 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 @NonNullFlow<T> <T extends Object> DatabaseKt.values(@NonNullQuery 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.