首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >getter "nameController“不是为"User”类型定义的

getter "nameController“不是为"User”类型定义的
EN

Stack Overflow用户
提问于 2022-07-25 18:38:02
回答 1查看 24关注 0票数 0

我试图将数据发送到防火墙,并遵循关于YouTube的教程,此教程向服务器发布数据的方法是创建"UserModel“类,如下所示:

代码语言:javascript
运行
复制
  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类的实例如下:

代码语言:javascript
运行
复制
//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“,我上次的搜索告诉我,不再使用这种方式了,如果有人能够提供正确的数据发布方式,我将感激不尽。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-25 18:45:08

使用

代码语言:javascript
运行
复制
userModel.name = nameController.text;

您编写了user.namecontroller,错误表示您创建的用户中没有名称控制器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73113885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档