提问人:FiFi 提问时间:6/2/2022 最后编辑:FiFi 更新时间:6/2/2022 访问量:836
使用提供程序包弹出页面时,获取对 null 值使用的 null 检查运算符
Getting null check operator used on a null value when popping page with Provider package
问:
我有一个执行此功能的登录按钮:
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 check 运算符用于 null 值,但这种情况只偶尔发生。其他时候,它工作正常。我该如何解决这个问题?
答:
0赞
FiFi
6/2/2022
#1
这解决了它。
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;
}
不知道为什么。也许有人可以填补我的空缺。
评论