Home > Back-end >  The argument type 'Function' can't be assigned to the parameter type 'void Funct
The argument type 'Function' can't be assigned to the parameter type 'void Funct

Time:12-31

'''
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,
    );
      }
  }
'''

While i was making an app an error occured 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.

CodePudding user response:

You need to chnage your function type to solve this issue

From

final Function toggleCheckboxState;

To

final ValueChanged<bool?> toggleCheckboxState;

CodePudding user response:

You use this

final  Function toggleCheckboxState;

to

final Function(Object?) toggleCheckboxState;

CodePudding user response:

Please refer to below code changes

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

  • Related