Gopal Rawat
Gopal Rawat

Reputation: 13

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

While I was making an app an error occurred says- The argument type 'Function' can't be assigned to the parameter type 'void Function(bool?)?'. inside Taskcheckbox stateless widget at onChanged : toggleCheckState says the function toggleCheckboxState can't be assigned.

class task_tile extends StatefulWidget {
  @override
  State<task_tile> createState() => _task_tileState();

}

class _task_tileState extends State<task_tile> {
  bool ischanged = false;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text("This is a box",style: TextStyle(
          decoration: ischanged ? TextDecoration.lineThrough:null
      ),
      ),
      trailing: Taskcheckbox(ischanged,(bool checkboxState) {
        setState(() {
          ischanged = checkboxState;
        });
      }),
    );
  }
}

class Taskcheckbox extends StatelessWidget {

final bool checkboxState;
final  Function toggleCheckboxState;

Taskcheckbox(this.checkboxState,this.toggleCheckboxState);
  @override
  Widget build(BuildContext context) {
    return Checkbox(
      activeColor: Colors.lightBlueAccent,
      value: checkboxState,
      onChanged:toggleCheckboxState,
    );
      }
  }

Upvotes: 0

Views: 1194

Answers (3)

Diwyansh
Diwyansh

Reputation: 3514

You need to chnage your function type to solve this issue

From

final Function toggleCheckboxState;

To

final ValueChanged<bool?> toggleCheckboxState;

Upvotes: 0

Tejaswini Dev
Tejaswini Dev

Reputation: 1469

Please refer to below code changes

@override
  Widget build(BuildContext context) {
    return Checkbox(
      activeColor: Colors.lightBlueAccent,
      value: checkboxState,
      onChanged: (bool val) {
        toggleCheckboxState();
      },
    );
  }

Upvotes: 0

keyur
keyur

Reputation: 181

You use this

final  Function toggleCheckboxState;

to

final Function(Object?) toggleCheckboxState;

Upvotes: 0

Related Questions