无法从登录屏幕导航到Jetpack Compose的主页。

huangapple go评论60阅读模式
英文:

I cant navigate to from login screen to home in jetpack compose?

问题

It seems you're facing a navigation issue in your Android app using Jetpack Compose. Based on your provided code, it's challenging to pinpoint the exact mistake. However, I can offer some general suggestions:

  1. Make sure you have the necessary dependencies and Jetpack Compose setup in your project.

  2. Ensure that you have the proper import statements at the top of your Kotlin files.

  3. Double-check that your ReaderNavigation composable is being used as the top-level composable in your app's entry point.

  4. Verify that ReaderLoginScreen is indeed calling the signInWithEmailAndPassword function correctly and that it triggers navigation to ReaderHomeScreen.

  5. In your signInWithEmailAndPassword function in LoginScreenViewModel, it seems like you're missing parentheses when calling the home lambda. It should be home() to execute the lambda.

  6. Ensure that there are no exceptions or errors being thrown that prevent the navigation from occurring. Check the logs for any relevant error messages.

  7. Verify that you've set up your navigation routes and destinations correctly in your app's navigation graph.

  8. Check for any potential logic errors in your code that might be preventing the navigation from occurring.

If you continue to face issues, please provide specific error messages or more details about where you believe the problem might be, and I can offer more targeted assistance.

英文:

I am developing Android app using Jetpack compose when I try to navigate from ReaderLoginScreen to Home screen it is not navigate to home screen

below my code in ReaderLoginScreen.kt

viewModel.signInWithEmailAndPassword(email, password){
                    navController.navigate(ReaderScreens.ReaderHomeScreen.name)
                }

where I am trying to navigate from ReaderLoginScreen to ReaderHomeScreen

below is my Navigation setup ReaderNavigation.kt

@Composable
fun  ReaderNavigation() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = ReaderScreens.SplashScreen.name){
       composable(ReaderScreens.SplashScreen.name){
           ReaderSplashScreen(navController = navController)
       }
        composable(ReaderScreens.LoginScreen.name){
            ReaderLoginScreen(navController = navController)
        }
        composable(ReaderScreens.ReaderHomeScreen.name){
            Home(navController = navController)
        }
    }
}

below my ReaderHomeScreen.kt

 import androidx.compose.material.Text
    import androidx.compose.runtime.Composable
    import androidx.navigation.NavController
    
    @Composable
    fun Home(navController: NavController) {
        Text(text = "I am home")
    
    }

below my LoginScreenViewModel.kt

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.launch

class LoginScreenViewModel : ViewModel() {
    // val loadingState = MutableStateFlow(LoadingState.IDLE)
    private val auth: FirebaseAuth = Firebase.auth
    private val _loading = MutableLiveData(false)
    val loading: LiveData<Boolean> = _loading

    fun signInWithEmailAndPassword(email: String, password: String, home:() -> Unit) = viewModelScope.launch {
        try {
            auth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener { task ->
                    if (task.isSuccessful) {
                       // Log.d("FB", "signInWithEmailAndPassword${task.result}")
                        home
                        // Login
                    } else {
                        Log.d("FB", "signInWithEmailAndPassword${task.result}")

                    }
                }

        } catch (ex: Exception) {
            Log.d("FB", "signInWithEmailAndPassword${ex.message}")
        }
    }

    fun createUserWithEmailAndPassword() {

    }


}

I want to know where I am making mistake

答案1

得分: 0

我通过以下方式调用函数来修复了这个问题:

viewModel.signInWithEmailAndPassword(email, password, home = {})
navController.navigate(ReaderScreens.ReaderHomeScreen.name)
英文:

I fixed the problem by calling function by following way

viewModel.signInWithEmailAndPassword(email, password, home = {})
                    navController.navigate(ReaderScreens.ReaderHomeScreen.name)

            }

huangapple
  • 本文由 发表于 2023年5月6日 23:24:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定