Home > Net >  isempty is showing Error under validator in Flutter
isempty is showing Error under validator in Flutter

Time:02-27

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. Please look at my code and suggest me a solution

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

Image: enter image description here

CodePudding user response:

 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.

  • Related