im stuck at this issue , The argument type 'Function' can't be assigned to the parameter type 'void Function(String?)?'.
the issue in bold, onSave and validator, any help will be appreciated.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'custom_text.dart';
class CustomTextFormField extends StatelessWidget {
final String text;
final String hint;
final Function onSave;
final Function validator;
CustomTextFormField({ required this.text, required this.hint , required this.onSave, required this.validator});
@override
Widget build(BuildContext context) {
var textFormField = TextFormField(
**onSaved: onSave ,
validator: validator ,**
decoration: InputDecoration(
hintText: hint,
hintStyle: TextStyle(color: Colors.black,
), fillColor: Colors.white
),
);
return Container(
child: Column(
children: [
CustomText(
text:text,
fontSize: 14,
color: Colors.grey.shade900,),
],
),
);
}
}
CodePudding user response:
Change your constructor argument from final Function onSave;
to final Function(String?)?
. (Same goes with the validator)
As onSave
and validator
gives you the value of type String back in the callback, if you want to add it as an argument, then you need to pass that value as well.
When you change the code as mentioned above, you can use your widget as:
CustomTextFormField(
text: "Testing",
hint: "This is a hint",
onSave: (value) => print("Value: $value"),
validator: (value) {
if (value!.isNotEmpty) {
return null;
} else {
return AppStrings.fieldCanNotBeEmpty;
}
}
);
For reference: