简体   繁体   中英

Android Firebase how to get child that contains a list

I'm getting this error when trying to get a child node trainer_profile from Firebase that contains a list my_pokemon .

W/ClassMapper: No setter/field for my_pokemon found on class com.tapmaxalf.poketest.model.TrainerProfile

My Firebase DB:

在此处输入图像描述

My valueEventListener looks like this:

    private fun getTrainerProfile(userId: String) {
    firebaseDatabase.child("trainer_profile").child(userId)
        .addValueEventListener(object : ValueEventListener {

            override fun onDataChange(snapshot: DataSnapshot) {

                snapshot.getValue(TrainerProfile::class.java).let { trainerProfile ->

                    if (trainerProfile != null) {
                        viewModelScope.launch (Dispatchers.IO){

                            _state.value = state.value.copy(trainerProfile = trainerProfile)

                            userDao.updateTrainerProfile(trainerProfile, userId)

                            canProfileBeAdded(trainerProfile.timeSentToDatabase)

                            addProfileCountdown(trainerProfile.timeSentToDatabase)
                        }
                    }
                }

            }

            override fun onCancelled(error: DatabaseError) {
                updateError(AuthErrors.GenericError.messageResource)
            }

        })
}

And my TrainerProfile

data class TrainerProfile(
val trainerName: String = "",
val location: String = "",
val trainerLevel: String = "",
val trainerCode: String = "",
val profileImage: String = "",
val gifts: Boolean = false,
val trade: Boolean = false,
val raid: Boolean = false,
val timeSentToDatabase: Long = 0,
val myPokemon: List<MyPokemon> = emptyList()
)

You are getting the following warning:

W/ClassMapper: No setter/field for my_pokemon found on class com.tapmaxalf.poketest.model.TrainerProfile

Because the list in your database is called my_pokemon , while in the database is called myPokemon , which is not correct. Both names must match. The simplest solution to this problem would be to use an annotations before your field like this:

@get:PropertyName("my_pokemon")
@set:PropertyName("my_pokemon")
@PropertyName("my_pokemon")
val myPokemon: List<MyPokemon> = emptyList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM