void Funtion(String) can't be assigned to void Funtion(String?)?

Viewed 41
Widget _buildName(){
   return TextFormField(
     decoration: InputDecoration(labelText: "Name"),
     validator: (String value){
       if(value.isEmpty){
         return 'Name is Required';
       }
     },
     onSaved: (String value){
       _name = value;
     }
   );
} 
1 Answers

If you check onSaved and validator methods, both provide nullable string

  • FormFieldSetter<String>? onSaved,
  • FormFieldValidator<String>? validator,

So it will be

TextFormField(
        decoration: InputDecoration(labelText: "Name"),
        validator: (String? value) {
          // add other condition
          if (value == null) {
            return 'Name is Required';
          }
          return null;
        },
        onSaved: (String? value) {
            _name = value; 
        });

More about TextFormField

Related