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

Issue

I am trying to pass a function using constructor arguments but it shows me the error mentioned in the title.

import 'package:flutter/material.dart';

class TaskTile extends StatefulWidget {
  @override
  _TaskTileState createState() => _TaskTileState();
}

class _TaskTileState extends State<TaskTile> {
  bool isChecked = false;

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(
        'This is a task',
        style: TextStyle(
          decoration: isChecked ? TextDecoration.lineThrough : null,
        ),
      ),
      trailing: TaskCheckbox(
        checkboxState: isChecked,
        toggleCheckboxState: (bool checkboxState) {
          setState(() {
            isChecked = checkboxState;
          });
        },
      ),
    );
  }
}

class TaskCheckbox extends StatelessWidget {
  final bool checkboxState;
  final Function toggleCheckboxState;

  TaskCheckbox(
      {required this.checkboxState, required this.toggleCheckboxState});

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

I searched the web for this but no luck over there.

so if anybody could help me out would me much appriciated.

Solution

Your final Function toggleCheckboxState; needs to have a bool? parameter.\

Try like this:

final Function(bool?) toggleCheckboxState;

You also need to change the code while assigning toggleCheckboxState like this,

toggleCheckboxState: (bool? checkboxState) {
  setState(() {
    isChecked = checkboxState!;
  });
},

bool? checkboxState would be required here since your type is Function(bool?)

Answered By – DarShan

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *