gpt4 book ai didi

kotlin - 调用只能在使用 Compose Navigation 的 @composable 函数的上下文中发生

转载 作者:行者123 更新时间:2023-12-05 02:25:55 25 4
gpt4 key购买 nike

您好,由于@composable 调用只能在@composable 函数的上下文中发生,我目前正在努力在Jetpack Compose 中进行导航。我有一个功能:

private fun signInResult(result: FirebaseAuthUIAuthenticationResult) {
val response = result.idpResponse
if (result.resultCode == RESULT_OK) {
user = FirebaseAuth.getInstance().currentUser
Log.e("MainActivity.kt", "Innlogging vellykket")
ScreenMain()
} else {
Log.e("MainActivity.kt", "Feil med innlogging" + response?.error?.errorCode)
}
}

与下面显示的导航类一起使用时,我只收到上面显示的错误消息,我该如何解决?

@Composable
fun ScreenMain(){
val navController = rememberNavController()

NavHost(navController = navController, startDestination = Routes.Vareliste.route) {

composable(Routes.SignUp.route) {
SignUp(navController = navController)
}

composable(Routes.ForgotPassword.route) { navBackStack ->
ForgotPassword(navController = navController)
}

composable(Routes.Vareliste.route) { navBackStack ->
Vareliste(navController = navController)
}

composable(Routes.Handlekurv.route) { navBackStack ->
Handlekurv(navController = navController)
}

composable(Routes.Profileromoss.route) { navBackStack ->
Profileromoss(navController = navController)
}

}
}

编辑完整代码如果你们想看的话,这里是类(class)的完整代码!

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
JetpackComposeDemoTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
LoginPage()
}
}
}
}

private var user: FirebaseUser? = FirebaseAuth.getInstance().currentUser
private lateinit var auth: FirebaseAuth



@Composable
fun LoginPage() {
Box(modifier = Modifier.fillMaxSize()) {
}
Column(
modifier = Modifier.padding(20.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {

Text(text = "Velkommen til ITGuys", style = TextStyle(fontSize = 36.sp))

Spacer(modifier = Modifier.height(20.dp))
Box(modifier = Modifier.padding(40.dp, 0.dp, 40.dp, 0.dp)) {
Button(
onClick = { signIn() },
shape = RoundedCornerShape(50.dp),
modifier = Modifier
.fillMaxWidth()
.height(50.dp)
) {
Text(text = "Logg inn")
}
}
}
}

private fun signIn() {
val providers = arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build()
)
val signinIntent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build()

signInLauncher.launch(signinIntent)
}



private val signInLauncher = registerForActivityResult(
FirebaseAuthUIActivityResultContract()
) {
res -> this.signInResult(res)
}

private fun signInResult(result: FirebaseAuthUIAuthenticationResult) {
val response = result.idpResponse
if (result.resultCode == RESULT_OK) {
user = FirebaseAuth.getInstance().currentUser
Log.e("MainActivity.kt", "Innlogging vellykket")
ScreenMain()
} else {
Log.e("MainActivity.kt", "Feil med innlogging" + response?.error?.errorCode)
}
}

我需要添加更多文本才能发布这么多代码,您可以忽略此文本,因为它只是为了能够发布。

最佳答案

如@z.y 所述,您可以传递带有 onFirebaseAuthSuccess 的 lambda。我还要补充一点,当您将 navController 传递到注册屏幕时,您需要传递的 lambda 回调应该类似于

onFirebaseAuthSuccess = { navController.navigate(Routes.Profileromoss.route) } - or whatever route you need

基于

composable(Routes.SignUp.route) {
SignUp(navController = navController)
}

我假设您的 signIn 屏幕是从可组合项的范围内调用的。如果您可以添加包含您如何调用 signInResult 函数的代码摘录,我们就可以确定这一点。

关于kotlin - 调用只能在使用 Compose Navigation 的 @composable 函数的上下文中发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74181211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com