Realtime Database Kotlin Extensions

Getting Started

To use the Firebase Realtime Database Android SDK with Kotlin Extensions, add the following to your app's build.gradle file:

// See maven.google.com for the latest versions
// This library transitively includes the firebase-database library
implementation 'com.google.firebase:firebase-database-ktx:$VERSION'

Features

Get an instance of FirebaseDatabase

Kotlin

val database = FirebaseDatabase.getInstance()
val anotherDatabase = FirebaseDatabase.getInstance(FirebaseApp.getInstance("myApp"))

Kotlin + KTX

val database = Firebase.database
val anotherDatabase = Firebase.database(Firebase.app("myApp"))

Get the FirebaseDatabase for the specified url

Kotlin

val database = FirebaseDatabase.getInstance(url)

Kotlin + KTX

val database = Firebase.database(url)

Get the FirebaseDatabase of the given FirebaseApp and url

Kotlin

val database = FirebaseDatabase.getInstance(app, url)

Kotlin + KTX

val database = Firebase.database(app, url)

Convert a DataSnapshot to a POJO

Kotlin

val snapshot: DataSnapshot = ...
val myObject = snapshot.getValue(MyClass::class.java)

Kotlin + KTX

val snapshot: DocumentSnapshot = ...
val myObject = snapshot.getValue<MyClass>()

Convert a DataSnapshot to generic types such as List or Map

Kotlin

val snapshot: DataSnapshot = ...
val typeIndicator = object : GenericTypeIndicator<List<Message>>() {}
val messages: List<Message> = snapshot.getValue(t)

Kotlin + KTX

val snapshot: DocumentSnapshot = ...
val messages: List<Message> = snapshot.getValue<List<@JvmSuppressWildcards Message>>()