How to initialize default values for GeoPoint in a data class

徘徊边缘 提交于 2021-02-05 12:11:24

问题


I'm trying to convert documents to objects from Firestore, but when I create my data class , I dont know how to initialize the GeoPoint with default values as it needs to be declared for an empty constructor

data class MapObj(val geopoint:Geopoint) //--> how to initialize it to get an empty constructor

The problem relies on this line

val obj = snapshot.toObject(MapObj::class.java)

Thanks


回答1:


The problem is that the Firestore SDK isn't finding a default, no argument constructor for MapObj. Kotlin data classes don't have one if all of their properties are declared with val. For properties declared val, Kotlin requires that their values be specified in the constructor, since they can't possibly change afterward.

If you must get a data class back from Firestore, you will have to make all of its properties optional var with a default value, so that Kotlin will generate a no-arg constructor for it.

data class MapObj(var geopoint: Geopoint? = null)


来源:https://stackoverflow.com/questions/57563216/how-to-initialize-default-values-for-geopoint-in-a-data-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!