Firebase Firestore toObject() with Kotlin

Related searches

I try to use Firebase Firestore in a Kotlin project. Everything is going fine except when I want to instantiate an object with DocumentSnapshot.toObject(Class valueType).

Here is the code :

FirebaseFirestore
    .getInstance()
    .collection("myObjects")
    .addSnapshotListener(this,
    { querySnapshot: QuerySnapshot?, e: FirebaseFirestoreException? ->

        for (document in querySnapshot.documents) {

            val myObject = document.toObject(MyObject::class.java)

            Log.e(TAG,document.data.get("foo")) // Print : "foo"
            Log.e(TAG, myObject.foo) // Print : ""
        }
    }
})

As you can see, when I use documentChange.document.toObject(MyObject::class.java), my object is instantiated but the inner fields are not set. I know that Firestore needs the model to have an empty constructor. So here is the model :

class MyObject {

    var foo: String = ""

    constructor(){}

}

Can somebody tell me what I'm doing wrong?

Thank you

You forgot to include the public constructor with arguments, or you can also just use a data class with default values, it should be enough:

data class MyObject(var foo: String = "")

DocumentSnapshot - Firebase, Convert a DocumentSnapshot to a POJO. Kotlin val snapshot: DocumentSnapshot = val myObject = snapshot.toObject(MyClass::class.java). Firebase provides a bunch of awesome services such as Firestore, Auth, Cloud Storage, Cloud Functions and Cloud Messaging. Learn how to use them in this tutorial course in which we’re making a fully fledged real-time chat app with everything you’d expect like sending images and getting notifications when we receive a new message.

In my case I was getting a NullPointerException because there wasn't a default constructor. Using a data class with default values fixed the error.

data class Message(
        val messageId : String = "",
        val userId : String = "",
        val userName : String = "",
        val text : String = "",
        val imageUrl : String? = null,
        val date : String = ""
)

Android: FireStore + Kotlin Part 2 (Firebase Firestore), To use the Cloud Firestore Android SDK with Kotlin Extensions, add the following to your app's build.gradle file: Kotlin val firestore = FirebaseFirestore. getInstance() val anotherFirestore = FirebaseFirestore. toObject(MyClass::class .java). 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

class MyObject {

    lateinit var foo: String

    constructor(foo:String) {
        this.foo = foo
    }

    constructor()

}

Firestore Kotlin Extensions, But it does not work as the object gets initialized but the field is not set. This is very similiar to Firebase Firestore toObject() with Kotlin. But I use� I. Technologies – Android Studio 3 – Kotlin 1.2.0 – Firebase Firestore 11.8.0 – FirebaseUI Firestore 3.1.0. II. Overview 1. Goal. We will build an Android App that supports showing, inserting, editing, deleting Notes from/to Cloud Firestore Database with with Android RecyclerView and FirebaseUI FirestoreRecyclerAdapter:

Firebase Firestore toObject() not working with data class – Onooks, toObject() always returns default boolean value #544 I have created a Kotlin data class to be written to Cloud Firestore. Can you see if the code snippet in https://firebase.google.com/docs/firestore/query-data/get-data#� I. Technologies – Android Studio 3 – Kotlin 1.2.0 – Firebase Firestore 11.8.0. II. Overview 1. Goal. We will build an Android App that supports showing, inserting, editing, deleting Notes from/to Cloud Firestore Database with RecyclerView:

DocumentSnapshot.toObject() always returns default boolean value , When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document changes. Note: While the code samples cover multiple languages, the text explaining the samples refers to the Web method names.

Firebase adds support to Coroutines through the kotlinx-coroutines-play-serviceslibrary. Firebase authentication and Firestore database operations can be suspended to ensure they’re not running

Comments
  • Using a data class is likely the best option since it creates publicly named getters/setters for each field. The Firestore data mapper looks for these (so to instantiate foo it needs a public setFoo())
  • And the failure mode for not having a default constructor is almost useless - you'll get "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference.
  • All Kotlin classes generate publicly named getters and setters for each public var field and publicly named getters for each public val field. Not only data classes.
  • To avoid the error, just create a data class and call the primary constructor in the empty constructor like constructor() : this("", null, 0)