value.length is showing error in flutter?

Issue

The length of the string.

Returns the number of UTF-16 code units in this string. The number of [runes] might be fewer, if the string contains characters outside the Basic Multilingual Plane (plane 0):

  TextFormField(
    obscureText: true,
    decoration: InputDecoration(
        hintText: "Enter Password", labelText: "Password"),
    validator: (value) {
      if (value != null && value.isEmpty) {
        return "Password cannot be empty";
      } else if (value.length < 6) {
        return "Password length should be atleast 6";
      }

      return null;
    },
  )

Solution

Restructure your if condition to :

if (value != null && value.isEmpty) {
  if (value.length < 6) {
    return "Password length should be atleast 6";
  }
  else {
    return null ;
  }
}
else {
  return "Password cannot be empty";
}

Answered By – Sathya Prakash

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *