0

I have made a form using Form() widget. It consists of 3 TextFormField- I have not forgotten to put formKey-

 body: Form(
        key: _formKey,

Fields are-

                  TextFormField(
                      initialValue: null,
                      controller: productNameController,
                      decoration: InputDecoration(
                        hintText: 'Product Name',
                      ),
                      validator: (value) {
                        print('Name: $value');
                        if (value == null || value.trim().isEmpty) {
                          return 'Product Name cannot be empty';
                        } else if (value.length > 10) {
                          return 'Product Name should not be more than 10 characters';
                        } else
                          return null;
                      },
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(
                        horizontal: MediaQuery.of(context).size.width * 0.03,
                        vertical: 5),
                    child: TextFormField(
                      initialValue: null,
                      keyboardType: TextInputType.numberWithOptions(
                          signed: false, decimal: false),
                      controller: productQuantityController,
                      decoration: InputDecoration(
                        hintText: 'Quantity',
                      ),
                      validator: (value) {
                        print('Quantity: $value');
                        if (value == null || value.trim().isEmpty) {
                          return 'Product Quantity cannot be empty';
                        } else if (num.parse(value) > 0 &&
                            (num.parse(value) is int ||
                                num.parse(value).roundToDouble() ==
                                    num.parse(value))) {
                          return 'Value should not be a fraction/negative integer';
                        } else
                          return null;
                      },
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(
                        horizontal: MediaQuery.of(context).size.width * 0.03,
                        vertical: 5),
                    child: TextFormField(
                      initialValue: null,
                      keyboardType: TextInputType.numberWithOptions(
                          signed: false, decimal: false),
                      controller: productPriceController,
                      decoration: InputDecoration(
                        hintText: 'Product Price (in numbers)',
                      ),
                      validator: (value) {
                        print('Price: $value');
                        if (value == null || value.trim().isEmpty) {
                          return 'Product Price cannot be empty';
                        } else if (num.parse(value) > 0 &&
                            (num.parse(value) is int ||
                                num.parse(value).roundToDouble() ==
                                    num.parse(value))) {
                          return 'Value should not be a fraction/negative integer';
                        } else
                          return null;
                      },
                    ),
                  ), 

In the onPressed of an ElevatedButton I am checking whether all form values are correct or not-

ElevatedButton(
                    onPressed: () {
                      // Validate returns true if the form is valid, or false otherwise.
                      if (_formKey.currentState!.validate()){
                        // If the form is valid, display a snack bar. In the real world,
                        // you'd often call a server or save the information in a database.
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('Processing Data')),
                        );
                        setState(() {
                          isLoading = true;
                        });
                        validateAndUploadData();
                      } else {
                        Fluttertoast.showToast(msg: 'Invalid Product details');
                        setState(() {
                          isLoading = false;
                        });
                      }
                    },
                    child: Text('Add Product'),
                  ),

The problem is that _formKey.currentState!.validate() does not return false even if the fields are empty and not tapped. It always returns true and proceeds to display the snack bar. I am not able to find any problem in the code. Please help!

ayush
  • 464
  • 5
  • 17
  • can you see if this helps you, apparently there is issue when you use form with listview https://github.com/flutter/flutter/issues/99383. https://stackoverflow.com/a/63357214/9930734 – Danny Rufus Jul 30 '22 at 07:55
  • 1
    I applied these changes and it worked! – ayush Jul 30 '22 at 13:22

0 Answers0