flutterdartnull-safety

3 errors in make components in flutter


I have 3 errors I tried all possible solutions
look at this

Widget defaultFormField ({
  @required TextEditingController? controller,
  @required TextInputType? keyboardType,
  @required IconData? prefix,
  @required String? label,
  VoidCallback? onChange,
  @required VoidCallback? validate,

}) => TextFormField(
    style: const TextStyle(
      color: Colors.amber,
    ),
    cursorHeight: 25.0,
    cursorColor: Colors.amber,
    controller: controller,
    keyboardType: keyboardType,
    decoration: const InputDecoration(
      prefixIcon: Icon(Icons.person_outline),
      labelText: label,
      labelStyle: TextStyle(
        color: Colors.amber,
      ),
      border: OutlineInputBorder(),
      enabledBorder: OutlineInputBorder(
        borderSide: BorderSide(
            color: Colors.amber
        ),
      ),
      focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(
          color: Colors.amber,
        ),
      ),
    ),
    onChanged: onChange,
    validator : validate,
);

these are problems

Invalid constant value.

The argument type 'void Function()?' can't be assigned to the parameter type 'void Function(String)?'.

The argument type 'void Function()?' can't be assigned to the parameter type 'void Function(String)?'.


Solution

    1. remove const from this line decoration: const InputDecoration( .

    2. required Function? onChange,

    3. required Function? validator,

    use onChanged like this

     onChanged: (value) {
             return onChanged != null ? onChanged(value) : null;
      },
    

    use validator like this

    validator: (value) {
                return validator != null ? validator(value) : null;
              },
    

    try above code it can solve your error. and remove @ from required.