Home > other >  3 errors in make components in flutter
3 errors in make components in flutter

Time:08-09

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)?'.

CodePudding user response:

  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.

CodePudding user response:

onChanged provide string on callback, do it like

Function(String)? onChange,

Also for validator it can be

 required Function(String?) validate,

validator : (value) =>  validate(value),

defaultFormField method

 Widget defaultFormField({
    required TextEditingController? controller,
    required TextInputType? keyboardType,
    required IconData? prefix,
    required String? label,
    Function(String)? onChange,
    required Function(String?) validate,
  }) =>
      TextFormField(
        style: const TextStyle(
          color: Colors.amber,
        ),
        cursorHeight: 25.0,
        cursorColor: Colors.amber,
        controller: controller,
        keyboardType: keyboardType,
        decoration: 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: (value) => validate(value),
      );
  • Related