3

I try to use Firebase Firestore in a Kotlin Android project. I have an issue when I try to instantiate an object with DocumentSnapshot.toObject(Class valueType). I am trying to read a single object from a collection called 'news' with id eq 1. I can read the data but the API won't let me put the document into my custom object.

News.kt

data class News(
     val id : String? = "",
     val value : String? = ""
)

HomeFragment.kt

val db = FirebaseFirestore.getInstance()

var newsItem = db.collection("news").document("1")
newsItem.get()
    .addOnSuccessListener { documentSnapshot ->

        android.util.Log.d("TAG", "${documentSnapshot.id} => ${documentSnapshot.data}")

        var newsTextView : TextView = view.findViewById(R.id.homeNewsText)
        val newsText = documentSnapshot.toObject<News>()
    }

Error in IDE is:

None of the following functions can be called with the arguments supplied. toObject(Class<News!>)   where T = News for   fun <T : Any!> toObject(valueType: Class<T!>): T? defined in com.google.firebase.firestore.DocumentSnapshot toObject(Class<News!>, DocumentSnapshot.ServerTimestampBehavior)   where T = News for   fun <T : Any!> toObject(valueType: Class<T!>, serverTimestampBehavior: DocumentSnapshot.ServerTimestampBehavior): T? defined in com.google.firebase.firestore.DocumentSnapshot

Thanks!

1
  • Fixed it by changing the toObject() call to: ``` val newsText = documentSnapshot.toObject<News>(News::class.java) ``` Not sure if it is intended to be used like that/ why this is necessary though Commented Jul 23, 2020 at 13:02

1 Answer 1

4

It looks like you're not including the Kotlin Extensions (KTX) version of the Firebase SDK, which defines the generic overload of toObject that you're trying to use.

I'll show both options below, since this seems to be quite common.


Using the regular Java/Android SDK

Include the SDK with:

implementation 'com.google.firebase:firebase-firestore:21.5.0'

Get the object from the data with:

documentSnapshot.toObject(News::class.java)

Using the Android SDK with Kotlin Extensions

Include the SDK with:

implementation 'com.google.firebase:firebase-firestore-ktx:21.5.0'

Get the object from the data with:

documentSnapshot.toObject<News>()
Sign up to request clarification or add additional context in comments.

4 Comments

That solved it. Thank's a lot! Might be a common error because the SDK was auto imported from the Firestore plugin within Android Studio.
Thanks for that info. That's good to know, because we were wondering where this came from. Let's hope that with your questions others can find the solution more easily.
I just fell into the exact same trap; Android Studio setup my deps for me & I followed the docs to the letter. Difficult to improve the docs; I guess you code prefix every KTX code sample with // requires: implementation 'com.google.firebase:firebase-firestore-ktx:21.5.0', but it's better to fix this on the Android Studio side; Kotlin project => import the KTX version.
Good point. 🙏 I'll ask around what we can do to improve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.