当弹出带有提供程序包的页面时,获取对空值使用的空检查操作符



我有一个登录按钮执行这个函数:

static Future loginPress(BuildContext context, TextEditingController emailController, TextEditingController passwordController) async {
Loader(context);
try {
await MyUser.signIn(emailController.text.trim(), passwordController.text.trim());
Navigator.of(context).pop();
Navigator.of(context).pushReplacementNamed(Routes.authPage);

} on CustomException catch (e) {
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.message.toString()))
);
}
return null;
}

和上面的Loader执行这个:

Future<dynamic> Loader(BuildContext context) {
return showDialog(
context: context, 
builder: (context){
return Center(child: CircularProgressIndicator(color: COLOR_PRIMARY),);
}
);
}

第一个代码块有MyUser。signIn功能如下:

static Future signIn(String email, String password) async {
if(email.isEmpty || password.isEmpty) throw CustomException('Either email or password field is empty');
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password);
} on FirebaseAuthException catch (e) {
print(e.message);
throw CustomException('Wrong password or email');
}
}

_dirty:false
_hadUnsatisfiedDependencies:false
_inDirtyList:false
_inheritedWidgets:null
_lifecycleState:_ElementLifecycle (_ElementLifecycle.defunct)
_owner:BuildOwner
_parent:null
_slot:null
_state:null
_widget:null
debugDoingBuild:false
debugIsDefunct:true
depth:121
dirty:false
hashCode:138
owner:BuildOwner
renderObject:null
runtimeType:Type (StatefulElement)
size:<Cannot get size of inactive element.
In order for an element to have a valid size, the element must be active, which means it is part of the tree.
Instead, this element is in the _ElementLifecycle.defunct state.
The size getter was called for the following element:
StatefulElement#0008a(DEFUNCT)
#0      Element.size.<anonymous closure> (package:flutter/src/widgets/framework.dart:4029:9)
#1      Element.size (package:flutter/src/widgets/framework.dart:4062:6)
#2      StatefulElement.Eval ()
#3      AuthViewModel.loginPress (package:better_health/view_model/auth_view_model.dart:49:17)
<asynchronous suspension>>
slot:null
state:<Null check operator used on a null value
#0      StatefulElement.state (package:flutter/src/widgets/framework.dart:4877:44)
#1      StatefulElement.Eval ()
#2      AuthViewModel.loginPress (package:better_health/view_model/auth_view_model.dart:49:17)
<asynchronous suspension>>
widget:<Null check operator used on a null value

在我执行Navigator.of(context).pop()的第一个代码块中,我得到了Null检查操作符在Null值上使用的错误,但这只会偶尔发生。其他时候,它工作得很好。我该如何解决这个问题?

解决了。

static Future loginPress(BuildContext context, TextEditingController emailController, TextEditingController passwordController) async {
late BuildContext dialogContext;
showDialog(
context: context, 
builder: (context) {
dialogContext = context;
return Center(child: CircularProgressIndicator(color: COLOR_PRIMARY),);
}
);
try {
await MyUser.signIn(emailController.text.trim(), passwordController.text.trim());
Navigator.of(dialogContext).pop();
Navigator.of(dialogContext).pushReplacementNamed(Routes.authPage);

} on CustomException catch (e) {
Navigator.of(context).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.message.toString()))
);
}
return null;
}

Idk为什么。也许有人能告诉我具体情况。

最新更新