【问题标题】:Iterate through firebase database using Kotlin使用 Kotlin 遍历 firebase 数据库
【发布时间】:2018-05-18 16:01:39
【问题描述】:

我已经创建了一个数组(我认为):

var recipeArray: List<RecipeTemplate> = mutableListOf() //RecpeTemplate is a class

我已经写出了基本的Firebase 代码并验证了它可以使用console.log 从数据库中打印出数据。

onChildAdded() 我现在想循环数据库并将数据添加到数组中。这就是我所拥有的:

override fun onChildAdded(snapshot: DataSnapshot?, p1: String?) {

            val children = snapshot!!.children 

            /*children.mapNotNullTo(recipeArray) {
                it.getValue(RecipeTemplate)<RecipeTemplate::class>
            } NOT SURE ABOUT THIS ONE*/


            children.forEach{
                var tempRecipe: RecipeTemplate? = null

                tempRecipe!!.recipeHeader = it.object["recipeHeaderFirebase"]
                tempRecipe!!.recipeText = it.object["recipeIngredientsTextFirebase"]
                tempRecipe!!.recipeImage = it.object["recipeImageFirebase"]

            }
        }

是 fb 数据的样本。

不确定最后一部分:it.object。我想它应该是别的东西......

【问题讨论】:

    标签: loops firebase firebase-realtime-database kotlin


    【解决方案1】:

    要迭代尝试以下操作:

    override fun onChildAdded(snapshot: DataSnapshot?, p1: String?) {
    
            val children = snapshot!!.children
            children.forEach {
            println(it.toString())
        }
    }
    

    这将在您的引用的直接子代中迭代并检索数据。

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 1970-01-01
      • 2019-07-30
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      相关资源
      最近更新 更多