6

I have a really weird issue, I have run the debugger and step through the code to see each element from Firestore

I have a list of products at my firestore database

Structure

shop -> shopId -> products -> productList

I have a data class as following

@Parcelize
data class Producto(
    val imagenProducto: String = "",
    val nombreProducto: String = "",
    val descProducto: String = "",
    val precio: Int = 0,
    val hasDescuento: Boolean = false,
    val tipoDescuento: Double = 0.0,
    val isCombo: Boolean = false
) : Parcelable

And now the code I use to filter by isCombo to create two lists that one will contain all the products (with a discount if needed) and combos (if the isCombo boolean is set to true)

Now, this is weird, because my code fetches all these documents and sends it to my view

suspend fun getProductosList(comercioId:String):Resource<MutableList<Producto>>{
        val productList = mutableListOf<Producto>()
        val snapshot = FirebaseFirestore.getInstance().collection("comercios").document(comercioId).collection("products").get().await()
        for(producto in snapshot){
            productList.add(producto.toObject(Producto::class.java))
        }
        return Resource.Success(productList)
    }

Here I have a subcollection where the final products of a shop (comercio) are.

Problem

So, here is what it happens, after I fetch all these documents, the list comes to my view with the data of each element of the list, but one value of the product does not come as espected.

I have place in one document isCombo = true but when this comes to my app, it places this value as false but the other boolean hasDescuento comes with the right value

What I have tried

  • clean and rebuild
  • invalidate cache / restart
  • step through with the debugger to see each element see if I was inserting my variable with a typo
  • check my reference to the data

Here are the outputs, this is how I have the data stored at my reference

enter image description here

here isCombo is set to true

Now, this is what ends up in my view when retrieving this document

enter image description here

So, here isCombo comes to false, but my other boolean hasDescuento comes with the right value from the database (for another different product), I was suspecting about the Parcelize annotation in my data class, but all the other data that's not isCombo is fetched normally

What could happen ?

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77

3 Answers3

10

I had the same problem and I found this solution

data class Producto(
    val imagenProducto: String = "",
    val nombreProducto: String = "",
    val descProducto: String = "",
    val precio: Int = 0,
    val hasDescuento: Boolean = false,
    val tipoDescuento: Double = 0.0,
    @field:JvmField // use this annotation if your Boolean field is prefixed with 'is'
    val isCombo: Boolean = false
)

https://firebase.google.com/docs/firestore/manage-data/add-data#kotlin+ktx_3

Rodrigo Rodrigues
  • 1,299
  • 1
  • 11
  • 9
5

It seems the property with is before Combo is mapped differently in Firestore and is mapped as just combo and not isCombo, so, changed the name of the property to hasCombo, cleaned and rebuilt the project and it worked.

Thanks to Alex Firebase Firestore toObject fails on Boolean property mapping

And will fill a suggestion for the Notes of boolean data type at the docs.

https://firebase.google.com/docs/firestore/manage-data/data-types

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • 5
    I suspect that `isCombo` is being interpreted as a getter for a boolean property named `combo`. Always keep in mind that Firebase/Firestore follows JavaBean patterns for property naming, and this is one of those. – Frank van Puffelen Mar 12 '20 at 00:26
1

Never, try to use isDone, isCombo, isDeclared,...etc fields (i.e. field must not be prefixed with is) , while creating helper Classes, else it will results to Firebase mapping conflicts with your getters() and setters() methods.

Error might occur at this line:

// Here bannerSnapshot is a QueryDocumentSnapshot object
CreateOfferObject product = bannerSnapshot.toObject(CreateOfferObject.class);

[NOTE: This error generally occurs in boolean fields.]

I've the same issue, and fixed it just by changing fields name, resetting getter() and setters().

Hope! it helps.

Mohd Asim
  • 724
  • 9
  • 7