Talha
Talha

Reputation: 12719

Firestore update array field in a Document

I have a document like below, in my document, have a Array . in array have Objects. How can i update new object to an Array.

As you see below i can add document in a colletion wit an array, but when i tried to update it gives error

java.lang.IllegalArgumentException: Invalid data. Unsupported type: com.test.data.modal.Product

What i tried;

        var pid = ""
        btnAdd.setOnClickListener {
            val list = ArrayList<Product>()
            list.add(Product("u1", "1", 1))
            list.add(Product("u2", "2", 1))
            list.add(Product("u3", "3", 1))

            val testObject = TestObject("Talha", "Kosen", list)
            FirebaseFirestore.getInstance().collection("Test")
                    .add(testObject)
                    .addOnCompleteListener { task ->
                        pid = task.result.id
                    }
        }

        btnUpdate.setOnClickListener {
            val list = ArrayList<Product>()
            list.add(Product("u1", "1", 1))
            list.add(Product("u2", "2", 1))
            list.add(Product("u3", "3", 1))
            list.add(Product("u4", "4", 4))

            FirebaseFirestore.getInstance()
                    .collection("Test")
                    .document(pid)
                    .update("product", list)
        }

document:

enter image description here

POJO:
@IgnoreExtraProperties
@Keep
class TestObject {

    var name: String = ""
    var surname: String = ""
    lateinit var productList: List<Product>

    constructor()

    constructor(name: String, surname: String, productList: List<Product>) {
        this.name = name
        this.surname = surname
        this.productList = productList
    }
}


@IgnoreExtraProperties
@Keep
class Product {
    var imagePath: String = ""
    var productUrl: String = ""
    var brand: Int = 0

    constructor()

    constructor(imagePath: String, productUrl: String, brand: Int) {
        this.imagePath = imagePath
        this.productUrl = productUrl
        this.brand = brand
    }
}

Upvotes: 2

Views: 3879

Answers (1)

Vince
Vince

Reputation: 3064

I have probably not the answer but this could help :

Upvotes: 1

Related Questions