Home > Enterprise >  The getter "nameController" isn't defined for the type "User" - flutter
The getter "nameController" isn't defined for the type "User" - flutter

Time:07-26

i am trying to send data to firestore and am following a tutorial on YouTube, this tutorial approach of posting data to the server is by creating "UserModel" class as follows:

  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,
    };
  }
}

Then in my registration form screen we create a method and declare an instance of the UserModel class as follows:

//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);
   }
  }

in the line where comment says "providing the fields values to the user model class" of this code the "user" is not accessing any "Controller" i created for the form fields my last search told me that this way is not used anymore, i will be thankful if anyone could provide the right way of posting data

CodePudding user response:

Use


userModel.name = nameController.text;

You wrote user.namecontroller and the error says there is no name controller in user that you created

  • Related