I am getting error
The argument type 'Function' can't be assigned to the parameter type 'String? Function(String?)?
after dart 2 for a form field widget.
full code:
class MyFormField extends StatelessWidget {
Function onTextChanged;
Function formValidator;
MyFormField(
{
required this.onTextChanged,
required this.formValidator,
});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
style: TextStyle(fontWeight: FontWeight.bold),
validator: formValidator, //***** Error Here
keyboardType: TextInputType.number,
onChanged: onTextChanged, //***** Error Here
),
);
}
}
Change it to this
Because the
onChangedfunction, is called when a value is entered into the form, and this value is astring?, because sometimes no value is entered, and you are not sure, which could be null if left empty. And your validation function and onchanged functions, need a string to compute and do logic. They get these strings from the(String? value)in your form fields.