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>()