【发布时间】:2022-01-16 08:30:21
【问题描述】:
我想在 Firebase 中插入一些数据。为此,我有一个不可组合的函数,在该函数中,我想调用 Toast.makeText 。 .在 .addOnSuccessListener 部分。 但是,我无法获得应该在 Toast.makeText 语句中的上下文
fun saveActivityToFB(
answer: String,
question: String,
id: String
) {
var db: DatabaseReference = Firebase.database.reference
val ques = Question(answer, question)
db.child("activity").child("test").child(id).setValue(ques)
.addOnSuccessListener {
Log.d("FB", "OK")
//problems with context here!!
Toast.makeText(context, "Successfully Added to FB", Toast.LENGTH_SHORT).show()
}
.addOnFailureListener {
Log.d("FB", "Not inserted into FB")
}
}
我知道为了从可组合函数中显示 Toast,我应该将上下文获取为:
val context = LocalContext.current
但不知道在这种情况下如何获取上下文。
【问题讨论】:
-
如果你想显示来自
saveActivityToFB本身的toast,你可以在这个函数中传递Context作为另一个参数。 -
请您这么好心,并在代码中显示您的建议。 .谢谢
-
是的,我按照你的建议得到了。我在理解 Context 方面遇到了很多问题。 . .谢谢
标签: android firebase kotlin firebase-realtime-database android-jetpack-compose