我在flutter应用程序中有一个登录页面,用于验证和注册用户。当我想将用户电子邮件地址保存到Firestore(注册后)时,出现错误:
flutter: Error: NoSuchMethodError: The method 'saveRegisterUserData' was called on null.
Receiver: null
Tried calling: saveRegisterUserData(email: "test@test.de", userID: "EVDnVFMkYeYd291Yzu2j2r8HLpC2")'
我为Firestore Functions创建了一个单独的dart文件。看起来像这样:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';
abstract class BaseFire {
Future <void> saveRegisterUserData({String userID, String email});
}
class Fire implements BaseFire {
final Firestore firestore = new Firestore();
DocumentReference usersdocuments;
Future <void> saveRegisterUserData({String userID, String email}) async {
usersdocuments = Firestore.instance.collection("Users").document(userID);
Map<String, String> registerdata = <String, String>{
"Email": email,
};
usersdocuments.setData(registerdata).whenComplete(() {
return print("Document Added");
}).catchError((error) => print(error));
}
}
我将这个文件导入我的登录页面并执行此功能:
import '../Firebase/firestore.dart';
class SignInPage extends StatefulWidget {
SignInPage({this.firestore});
final BaseFire firestore;
@override
State<StatefulWidget> createState() {
return _SignInPageState();
}
}
class _SignInPageState extends State<SignInPage> {
String _email;
String _password;
...
void submitRegister() async {
if (validateAndSave()) {
try {
String userId = await widget.auth.createUserWithEmailAndPassword(_email, _password);
await widget.firestore.saveRegisterUserData(userID: userId, email: _email);
} catch (error) {
print(error);
}
}
}
}
我的错误在哪里?谢谢您的帮助
答案 0 :(得分:1)
您在以下位置具有自动的类成员变量初始化
SignInPage({this.firestore});
{}将参数设为可选,除非您确实打算将
firebase //variable
可选或具有默认值,请勿将其括在{}中,只需将其删除即可。您会在使用SignInPage的位置收到任何错误消息,只需修复参数即可,希望一切正常。