Ad

Adding Value To A List In Firebase Database

I'm working on a Kotlin app. It requires me to create my custom data type (User.Item) that represents an item. I'm using a mutable list to work with it. However, I can't add to the list. I'm using Firebase Database

I've tried various Firebase tutorials but they don't seem to be giving me what I need.

val item = User.Item(firstName,expiry,userId,null,null)
mDatabaseReference!!.child(userId).child("items")

//User.Item
data class Item(
        var name: String?="",
        var expiry: String?=null,
        var donator: String?=null,
        var pickupGuy: String?=null,
        var location: Location?=null
    )

//User.user
data class user(var name: String? = "",
                    var password: String? = "",
                    var email: String? = "",
                    var phoneNumber: String?="",
                    var uid: String? = "",
                    var volunteer: Boolean?=false,
                    var items: List<Item>?= emptyList()

    )

mDatabaseReference!!.child(userId).child("items") gives me a data reference. I would like it to be a list that I can manipulate. I've tried updateChildren(), setting it to a list and then pushing and then using that as a new value but nothing works.

Picture of my database

The adding is done in AddItemActivity on line 94-95 Github link: https://github.com/dl1683/ShareEats

Ad

Answer

So I worked using the newdata push

val item = User.Item(firstName,expiry,userId,null,null)
val newData=mDatabaseReference!!.child(userId).child("items").push()
newData.setValue(item)
Ad
source: stackoverflow.com
Ad