isempty is showing Error under validator in Flutter

Viewed 153

Hi I am very New in Flutter and trying to learn new stuff i am facing issue on value.isEmpty i don't know why it is showing error in attribute validator.

  TextFormField(
    decoration: const InputDecoration(
      icon: const Icon(Icons.phone),
      hintText: 'Enter a phone number',
      labelText: 'Phone',
    ),
    validator: (value) {
      if (value.isEmpty) {
        return 'Please enter phone number';
      }
      return null;
    },
  ),

enter image description here

2 Answers
 TextFormField(
    decoration: const InputDecoration(
      icon: const Icon(Icons.phone),
      hintText: 'Enter a phone number',
      labelText: 'Phone',
    ),
    validator: (value) {
      if (value!.isEmpty) {
        return 'Please enter phone number';
      }
      return null;
    },
  ),

use value!.isEmpty instead of value.isEmpty.

Try this:

TextFormField(
    decoration: const InputDecoration(
      icon: Icon(Icons.phone),
      hintText: 'Enter a phone number',
      labelText: 'Phone',
    ),
    validator: (value) {
      if (value!.isEmpty) {
        return 'Please enter phone number';
      }
      return null;
    },
  ),
Related