Flutter flatterfirebase Auth抛出NoSuchMethodError:getter';数据';被调用为空
在使用Flutter flatterfirebase Auth抛出NoSuchMethodError:getter';数据';被调用为空,flutter,firebase-authentication,Flutter,Firebase Authentication,在使用firebaseAuth.createUserWithEmailAndPassword注册电子邮件应用程序的过程中,当我尝试在部分执行上传或保存到prefs时,它会抛出以下错误: NoSuchMethodError:对null调用了getter“data”。 因此,我可以通过导航到一个新屏幕并将用户TextFormField输入的处理推迟到那个屏幕来解决这个问题,但这很混乱,让我很烦 在中做任何大事。那么似乎有问题,但我真的不知道是什么导致了问题,或者实际上最好的方法是什么来解决这类问题,
firebaseAuth.createUserWithEmailAndPassword注册电子邮件应用程序的过程中,当我尝试在部分执行上传或保存到prefs时,它会抛出以下错误:
NoSuchMethodError:对null调用了getter“data”。
因此,我可以通过导航到一个新屏幕并将用户TextFormField输入的处理推迟到那个屏幕来解决这个问题,但这很混乱,让我很烦
在中做任何大事。那么似乎有问题,但我真的不知道是什么导致了问题,或者实际上最好的方法是什么来解决这类问题,以便将来更清楚。感谢您的教育
void registerToFb() {
firebaseAuth
.createUserWithEmailAndPassword(
email: emailController.text, password: passwordController.text)
.then((result) async {
Person user = new Person();
user.email = emailController.text;
user.firstName = firstNameController.text;
user.surname = surnameController.text;
user.postcode = postcodeController.text;
user.password = passwordController.text;
user.city = cityController.text ?? "Edinburgh";
user.firebaseId = result.user.uid;
Map<String, dynamic> firebaseUpload = user.toMap();
print("Attempting to reduce upload");
firebaseUpload.removeWhere((key, value) => value == null);
user.country = "GB";
String path = "${user.country}/${user.city}/People";
print("Attempting record upload");
DocumentReference autoId =
await myFirestore.collection(path).add(firebaseUpload);
user.personId = autoId.id;
user.saveToPrefs(prefs);
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => MyHomePage()));
}).catchError((err) {
print("Login thrown an error...\n${err.toString()}");
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Error 10"),
content: Text("${err.toString()}"),
actions: [
ElevatedButton(
child: Text("Ok"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
});
void registerToFb(){
firebaseAuth
.createUserWithEmailAndPassword(
电子邮件:emailController.text,密码:passwordController.text)
.then((结果)异步{
个人用户=新的个人();
user.email=emailController.text;
user.firstName=firstNameController.text;
user.name=namescontroller.text;
user.postcode=postcodeController.text;
user.password=passwordController.text;
user.city=cityController.text??“爱丁堡”;
user.firebaseId=result.user.uid;
Map firebaseUpload=user.toMap();
打印(“试图减少上传”);
firebaseUpload.removeWhere((键,值)=>value==null);
user.country=“GB”;
字符串路径=“${user.country}/${user.city}/People”;
打印(“试图上传记录”);
文档引用自动ID=
等待myFirestore.collection(路径).add(firebaseUpload);
user.personId=autoId.id;
user.saveToPrefs(prefs);
导航器。更换(
context,MaterialPageRoute(builder:(context)=>MyHomePage());
}).catchError((err){
打印(“登录时抛出错误…\n${err.toString()}”);
显示对话框(
上下文:上下文,
生成器:(BuildContext上下文){
返回警报对话框(
标题:文本(“错误10”),
内容:文本(${err.toString()}),
行动:[
升降按钮(
孩子:文本(“Ok”),
已按下:(){
Navigator.of(context.pop();
},
)
],
);
});
});
我的建议是完全删除.then()回调,因为您将其声明为异步。更好的方法是使整个函数异步,这样您就可以直接在其中执行所有异步代码
使函数异步
将.then()回调更改为简单的等待,并将结果存储在结果变量中
我强烈建议使用try/catch块围绕此语句,以避免未处理的错误:
您可能会出现此错误,因为您将.then()调用标记为异步,因为它随后异步执行,并且数据可能还不在“那里”,但我不确定这一点
为这个深思熟虑的答案干杯。它解决了这个问题,结果是当我将我的用户对象转换为Firebase上载的地图时,出现了一个bug。这个bug没有在日志中标记,直到我按照你说的那样做,异步了整个函数并删除了。然后。下一个问题现在,hurrah@Irsvmb!
void registerToFb() async { ...
var result = await firebaseAuth.createUserWithEmailAndPassword(email: emailController.text, password: passwordController.text);
try {
var result = await firebaseAuth.createUserWithEmailAndPassword(
email: emailController.text,
password: passowrdController.text
);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('password too weak.');
} else if (e.code == 'email-already-in-use') {
print('email already exists');
}
} catch (e) {
print(e);
}