Firestore Kotlin Extensions

Getting Started

To use the Cloud Firestore 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-firestore library
implementation 'com.google.firebase:firebase-firestore-ktx:$VERSION'

Features

Get an instance of FirebaseFirestore

Kotlin

val firestore = FirebaseFirestore.getInstance()
val anotherFirestore = FirebaseFirestore.getInstance(FirebaseApp.getInstance("myApp"))

Kotlin + KTX

val firestore = Firebase.firestore
val anotherFirestore = Firebase.firestore(Firebase.app("myApp"))

Convert a DocumentSnapshot field to a POJO

Kotlin

val snapshot: DocumentSnapshot = ...
val myObject = snapshot.get("fieldPath", MyClass::class.java)

Kotlin + KTX

val snapshot: DocumentSnapshot = ...
val myObject = snapshot.get<MyClass>("fieldPath")

Convert a DocumentSnapshot to a POJO

Kotlin

val snapshot: DocumentSnapshot = ...
val myObject = snapshot.toObject(MyClass::class.java)

Kotlin + KTX

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

Convert a QuerySnapshot to a list of POJOs

Kotlin

val snapshot: QuerySnapshot = ...
val objectList = snapshot.toObjects(MyClass::class.java)

Kotlin + KTX

val snapshot: QuerySnapshot = ...
val objectList = snapshot.toObjects<MyClass>()

Setup Firestore with a local emulator

Kotlin

val settings = FirebaseFirestoreSettings.Builder()
                   .setHost("10.0.2.2:8080")
                   .setSslEnabled(false)
                   .setPersistenceEnabled(false)
                   .build()

firestore.setFirestoreSettings(settings)

Kotlin + KTX

firestore.firestoreSettings = firestoreSettings {
    host = "http://10.0.0.2:8080"
    isSslEnabled = false
    isPersistenceEnabled = false
}