空的Kotlin对象构造函数以使用Firebase [英] Empty constructor for kotlin object to use Firebase
本文介绍了空的Kotlin对象构造函数以使用Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从Kotlin将对象保存在Firebase数据库中,
我不觉得提供默认的空构造函数并将值设置为nullable
,必须更改我的所有代码bc.
I'm trying to save an object inside Firebase database from Kotlin,
I dont feel right providing a default empty constructor and put values as nullable
, having to change all my code bc of this.
我的课:
class Video(var id: String, var url: String, var owner: User) : {
constructor() : this("", "", User("", "", ""))
}
Firebase推送:
Firebase push:
FirebaseDatabase.getInstance().reference.child("public").push().setValue(video)
错误:
缺少没有参数的构造函数
is missing a constructor with no arguments
是否有更好的解决方案?
Is there a better solution for this?
推荐答案
使用默认参数:
class Video(var id: String = "", var url: String = "",
var owner: User = User("", "", ""))
(也可以考虑使用val
).
这篇关于空的Kotlin对象构造函数以使用Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文