Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

im have prorblem with formstate retrun false when validation and it should be true who can solve this problem ?

i'm new in Flutter and i am trying to create sign-in form validation but always get false when i call formKey.currentState.validate(). formKey is global key form state I am defining the key for my form like so:

 class LoginState extends State<LogIn> {
  final name = TextEditingController();
  // TextEditingController name = new TextEditingController();
  final password = TextEditingController();
  final email = TextEditingController();
  // TextEditingController email = new TextEditingController();
  // TextEditingController password = new TextEditingController();
  TextEditingController cofipassword = new TextEditingController();
  GlobalKey<FormState> formstatesingup = new GlobalKey<FormState>();
  GlobalKey<FormState> formstatesingin = new GlobalKey<FormState>();

the function look like :

  singin() {
    var formdate = formstatesingin.currentState;
    if (formdate.validate()) {
      // formdate.save();
      // var data = {'email': email.text, 'password': password.text};
      // var response = await http.post(
      //     Uri.parse('https://onlinestore1997.000webhostapp.com/login.php'),
      //     body: data);
      // var responsebody = jsonDecode(response.body);
      // if (responsebody['status'] == 'success') {
      //   print(responsebody['username']);
      print('vaildate');
    } else {
      print(Text(email.text));
      print(formstatesingin.currentState.validate());
      print('not vaildate');

formbox :

 Center buildFormBoxSingIn(double mdw) {
    return Center(
      child: AnimatedContainer(
        duration: Duration(milliseconds: 300),
        curve: Curves.easeInOutBack,
        margin: EdgeInsets.only(top: 20),
        height: 250,
        width: mdw / 1.2,
        decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [BoxShadow(color: Colors.black, spreadRadius: 1)]),
        child: Form(
          key: formstatesingin,
          child: Container(
            margin: EdgeInsets.only(top: 20),
            padding: EdgeInsets.all(10),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  //start
                  Text(' Email ',
                      style: TextStyle(
                        color: Colors.blue[800],
                  SizedBox(
                    height: 10,
                  buildTextFormFieldAll(
                      "Enter your Email", false, email, vaildglobal),
                  //end Email
                  SizedBox(
                    height: 10,
                  //start
                  Text(' Passworld ',
                      style: TextStyle(
                        color: Colors.blue[800],
                  SizedBox(
                    height: 10,
                  buildTextFormFieldAll(
                      "Enter passworld", true, password, vaildglobal),
                  //end Email

text form like this :

  TextFormField buildTextFormFieldAll(String myhinttext, bool pass,
      TextEditingController myController, myvalid) {
    return TextFormField(
        validator: myvalid,
        controller: myController,
        obscureText: pass,
        decoration: InputDecoration(
          contentPadding: EdgeInsets.all(4),
          hintText: myhinttext,
          filled: true,
          fillColor: Colors.grey[100],
          enabledBorder: OutlineInputBorder(
              borderSide: BorderSide(
                  color: Colors.grey[500], style: BorderStyle.solid, width: 1)),
          focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(
                  color: Colors.blue[500], style: BorderStyle.solid, width: 1)),
                i don't got error in vs but the error in validator it false even the  email and password full of text and i don't know why
– gemma
                Apr 24, 2021 at 19:19

validator: (value) {if (value == null || value.isEmpty) return "Invalid mobile number"; return " " //is not correct

//Write Like this

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Aug 11, 2022 at 11:30

That's how I do it:

                  onPressed: () {
                  final isValidForm = _yourFormKey.currentState!.validate();
                  if (isValidForm) {
                  //Do something

And make sure that you are doing the validation correctly:

                    validator: (String? value) {
                    if (value == null || value.isEmpty) {
                      return 'Email Required';
                    return null;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.