jetpackcompose碎片发行问题
我需要在我的SignScreen的Oncreate方法中调用功能,但没有任何适用的位置。我无法从任何地方调用CurrentUserCheck
函数。
我尝试过的内容:
在
init
中调用它在ViewModel中。但是问题在于它扔了nullpoInterException
navcontroller
此处。在
MainActivity
和Mytheme
中调用它,但我在这些范围中遇到了许多奇怪的问题。
ViewModel :
@HiltViewModel
class SignViewModel (@ApplicationContext context: Context) : ViewModel() {
val auth = FirebaseAuth.getInstance()
init {
currentUserCheck(NavController(context))
}
fun signIn(email: String?, password: String?, context: Context, navController: NavController) {
if (email != null && email.isNotBlank() && password != null && password.isNotBlank()) {
auth.signInWithEmailAndPassword(email, password).addOnSuccessListener {
navController.navigate(ScreenHolder.ProfileScreen.route) {
popUpTo(ScreenHolder.SigningScreen.route) {
inclusive = true
}
}
}.addOnFailureListener {
Toast.makeText(context, it.localizedMessage, Toast.LENGTH_LONG).show()
}
} else {
Toast.makeText(
context,
"Lütfen email ve şifre alanlarını boş bırakmayınız.",
Toast.LENGTH_LONG
).show()
}
}
fun signUp(email: String?, password: String?, context: Context, navController: NavController) {
if (email != null && email.isNotBlank() && password != null && password.isNotBlank()) {
auth.createUserWithEmailAndPassword(email, password)
.addOnSuccessListener {
navController.navigate(ScreenHolder.ProfileScreen.route) {
popUpTo(ScreenHolder.ProfileScreen.route) {
inclusive = true
}
}
}
} else {
Toast.makeText(
context,
"Lütfen email ve şifre alanlarını boş bırakmayınız.",
Toast.LENGTH_LONG
).show()
}
}
fun currentUserCheck(navController: NavController) {
if (auth.currentUser != null) {
navController.navigate(ScreenHolder.ProfileScreen.route) {
popUpTo(ScreenHolder.ProfileScreen.route) {
inclusive = true
}
}
}
}
}
I need to call a function in my SignScreen's onCreate method but there is not any applicable place for this. I can't call currentUserCheck
function from anywhere.
What i tried :
Calling it in
init
block in viewModel. But the problem was it throwingnullPointerException
forNavController
here.Calling it in
MainActivity
andMyTheme
but i faced many weird issues in these scopes.
ViewModel:
@HiltViewModel
class SignViewModel (@ApplicationContext context: Context) : ViewModel() {
val auth = FirebaseAuth.getInstance()
init {
currentUserCheck(NavController(context))
}
fun signIn(email: String?, password: String?, context: Context, navController: NavController) {
if (email != null && email.isNotBlank() && password != null && password.isNotBlank()) {
auth.signInWithEmailAndPassword(email, password).addOnSuccessListener {
navController.navigate(ScreenHolder.ProfileScreen.route) {
popUpTo(ScreenHolder.SigningScreen.route) {
inclusive = true
}
}
}.addOnFailureListener {
Toast.makeText(context, it.localizedMessage, Toast.LENGTH_LONG).show()
}
} else {
Toast.makeText(
context,
"Lütfen email ve şifre alanlarını boş bırakmayınız.",
Toast.LENGTH_LONG
).show()
}
}
fun signUp(email: String?, password: String?, context: Context, navController: NavController) {
if (email != null && email.isNotBlank() && password != null && password.isNotBlank()) {
auth.createUserWithEmailAndPassword(email, password)
.addOnSuccessListener {
navController.navigate(ScreenHolder.ProfileScreen.route) {
popUpTo(ScreenHolder.ProfileScreen.route) {
inclusive = true
}
}
}
} else {
Toast.makeText(
context,
"Lütfen email ve şifre alanlarını boş bırakmayınız.",
Toast.LENGTH_LONG
).show()
}
}
fun currentUserCheck(navController: NavController) {
if (auth.currentUser != null) {
navController.navigate(ScreenHolder.ProfileScreen.route) {
popUpTo(ScreenHolder.ProfileScreen.route) {
inclusive = true
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案
正如@pztar所说,我在ViewModel中声明了控制操作为状态的身份验证,然后我在我的Composobable
signscreen
中使用 启动效果及已解决。Solution
As @Pztar said i declare the authentication controlling operation as a state in viewModel and then i observed it in my Composable
SignScreen
with LaunchEffect and it is solved.