Home > OS >  The argument type 'dynamic Function(bool)?' can't be assigned to the parameter type &
The argument type 'dynamic Function(bool)?' can't be assigned to the parameter type &

Time:09-22

recently I faced this issue but I don't have any idea where is it. I am getting this error. The argument type 'dynamic Function(bool)?' can't be assigned to the parameter type 'void Function(bool?)?'

Where am I making mistake?

  class GroceryTile extends StatelessWidget {
  GroceryTile({Key? key, required this.item, required this.onComplete})
      : textDecoration = item.isComplete != null
            ? TextDecoration.lineThrough
            : TextDecoration.none,
        super(key: key);

  final GroceryItem item;
  final  Function(bool) onComplete;
  final TextDecoration textDecoration;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100.0,
      // TODO: 20 Replace this color
      color: Colors.red,
    );
  }

  

  Widget buildCheckbox() {
    return Checkbox(
      value: item.isComplete,
      onChanged: **onComplete** //error is occurring here
    );
  }
}

CodePudding user response:

final void Function(bool?)? onComplete

Checkbox is a void function so u should also put void on you declaration for OnComplete if your not using nullsafety just remove this sign ?

CodePudding user response:

Your member onComplete is of type

void Function(bool)

It needs to be

void Function(bool?)

It must be able to deal with a null parameter.

CodePudding user response:

The error occured because checkbox onChanged methods needs a Function(bool?)? but you have supplied a dynamic Function(bool)?. void Function(bool?)? means that the value for a checkbox onChanged method can be a null or a function with a null parameter.

simple changing the signature of your onChange method will fix the issue.

final void Function(bool?) onComplete;

Better check the documentation in these kind of errors to find out the correct function signatures.

  • Related