简体   繁体   English

如何使用 Firebase 和 Kotlin 在文本(Jetpack Compose)中显示当前用户名?

[英]How to display current username in Text (Jetpack Compose) using Firebase and Kotlin?

So, I have a Firebase Realtime Database with user's name.所以,我有一个带有用户名的 Firebase 实时数据库。 App should retrieve username of who tries to log in.应用程序应检索尝试登录的用户名。

Firebase Realtime Database Firebase 实时数据库

I need to display current user's "name" in Text() with Jetpack Compose.我需要使用 Jetpack Compose 在Text()中显示当前用户的“名称”。 To the place where &username in the picture below.到下图中&username所在的地方。

Where to put user's name在哪里放置用户名

This is what I have tried:这是我试过的:

private val rootRef = FirebaseDatabase.getInstance().reference
private val uid = FirebaseAuth.getInstance().currentUser!!.uid
private val uidRef = rootRef.child("users").child(uid)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        MyPassTheme {
            initFirebase()
            WelcomeScreen()

            uidRef.get().addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val snapshot = task.result
                    val username = snapshot?.child("name")?.getValue(String::class.java)
                    Toast.makeText(baseContext, "$username",
                        Toast.LENGTH_SHORT).show()
                }
            }

But in WelcomeScreen() I cannot update $username from addOnCompleteListener .但是在WelcomeScreen()中,我无法从addOnCompleteListener更新$username

@Composable
private fun WelcomeScreen() {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(blue69),
    )
    Column(
        modifier = Modifier
            .height(605.dp)
            .fillMaxSize()
            .padding(5.dp),
        verticalArrangement = Arrangement.SpaceEvenly,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Card(
            shape = RoundedCornerShape(20.dp),
            elevation = 10.dp,
            modifier = paddingModifier
                .height(2.dp)
        ) {
            Text(text = widthOnTopBar, modifier = paddingModifier)
        }
    }
    Column(
        modifier = Modifier
            .padding(30.dp)
            .width(1500.dp)
            .height(270.dp)
            .fillMaxWidth()
            .wrapContentSize(Alignment.BottomCenter)
    ) {
        Text(
            modifier = Modifier,
            text = "&username",
            textAlign = TextAlign.Center,
            style = MaterialTheme.typography.h4.copy(
                shadow = Shadow(
                    offset = Offset(2f, 2f),
                    blurRadius = 1f
                ),
                color = Color.White,
                fontSize = 45.sp,
                fontFamily = montserrat_bold
            )
        )
    }

I suppose there is something about data classes and OOP, but i do not know how to do it.我想有一些关于数据类和 OOP 的东西,但我不知道该怎么做。

Not sure if this would work, but you can try this.不确定这是否可行,但您可以试试这个。

We have added a userName state that will be updated from your addOnCompleteListener callback, and when it changes, it will update your WelcomeScreen我们添加了一个userName名 state,它将从您的addOnCompleteListener回调中更新,当它发生变化时,它将更新您的WelcomeScreen

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        MyPassTheme {

            var userName by remember { mutableStateOf("") }
            ...
            WelcomeScreen(userName)

            uidRef.get().addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val snapshot = task.result
                    userName = snapshot?.child("name")?.getValue(String::class.java)
                    Toast.makeText(
                        baseContext, "$username",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }
}

and your modified WelcomeScreen和您修改后的WelcomeScreen

@Composable
private fun WelcomeScreen(userName: String) {

    ...

    Column(
         ...
    ) {
        Text(
            ...
            text = userName,
            ...
        )
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Firebase 和 Jetpack Compose 进行谷歌身份验证 - Google authentication with Firebase and Jetpack Compose Jetpack compose firebase 匿名账户转永久账户(Gmail) - Jetpack compose firebase convert anonymous account to permanent account (Gmail) 如何显示来自 firebase 的当前用户数据? - How to display current User data from firebase? Android中如何使用Kotlin上传图片到Firebase存储 Q - How to upload an Image to Firebase Storage using Kotlin in Android Q 如何使用 Jetpack Compose UI 正确跟踪 Android 上的屏幕视图? - How to properly track screen view on Android with Jetpack Compose UI? 如何在从 Firebase android kotlin 获取数据时显示加载的项目 - How to display items loading while data is being fetched from Firebase android kotlin Jetpack Compose Firebase 使用 Google 登录,没有错误但无法正常工作,我错过了什么 - Jetpack Compose Firebase Signin With Google, no error but not working, what am I missing 如何在 Firebase 中设置自定义用户名? - How to set a custom username in Firebase? 如何通过 Firebase ML 套件文本识别扫描七段显示器? - How To Scan a Seven Segment Display by Firebase ML kit Text Recognition? 如何在片段 class 的文本视图中显示来自 firebase 的值 - how to display a value from firebase in a text view in a fragment class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM