我试图将数据发送到防火墙,并遵循关于YouTube的教程,此教程向服务器发布数据的方法是创建"UserModel“类,如下所示:
class UserModel {
String? uid;
String? name;
String? email;
String? phone;
String? province;
String? dateOfBirth;
//parsing data to JSON
UserModel(
{this.uid,
this.name,
this.email,
this.phone,
this.province,
this.dateOfBirth});
//Access and fetching data from the server (cloud firestore)
factory UserModel.fromMap(map) {
return UserModel(
uid: map['uid'],
name: map['name'],
email: map['email'],
phone: map['phone'],
province: map['province'],
dateOfBirth: map['dateOfBirth'],
);
}
//Sending data to server (cloud firestore)
Map<String, dynamic> toMap() {
return {
'uid': uid,
'name': name,
'email': email,
'phone': phone,
'province': province,
'dateOfBirth': dateOfBirth,
};
}
}然后,在我的注册表单屏幕中,我们创建一个方法并声明UserModel类的实例如下:
//Sign up method (when user clicks on sign up this method will be invoked)
void signUp(String email, String password) async {
if (_formKey.currentState!.validate()) {
await _auth
.createUserWithEmailAndPassword(email: email, password: password)
.then((value) => { postDataToFirestore();})
.catchError((e) {
Fluttertoast.showToast(msg: e!.message);
});
}
}
postDataToFirestore() async {
//Creating Instance of firestore from firebase
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
User? user = _auth.currentUser;
//Creating an instance of UserModel Class
UserModel userModel = UserModel();
//providing the fields values to the user model class
userModel.name = user!.nameController.text; // Error Here
await firebaseFirestore
.collection("users")
.doc(user.uid)
.set(userModel.toMap());
Fluttertoast.showToast(msg: "Account created successfully :) ");
Navigator.pushAndRemoveUntil(
(context),
MaterialPageRoute(builder: (context) => HomeScreen()),
(route) => false);
}
}在注释中写着“向此代码的用户模型类提供字段值”时,“用户”没有访问我为表单字段创建的任何"Controller“,我上次的搜索告诉我,不再使用这种方式了,如果有人能够提供正确的数据发布方式,我将感激不尽。
发布于 2022-07-25 18:45:08
使用
userModel.name = nameController.text;您编写了user.namecontroller,错误表示您创建的用户中没有名称控制器。
https://stackoverflow.com/questions/73113885
复制相似问题