'Closure call with mismatched arguments: function '_TaskTileState.checkboxCallback'

I'm getting this error in my code

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building TaskCheckbox(dirty):
Closure call with mismatched arguments: function '_TaskTileState.checkboxCallback'
Receiver: Closure: (bool) => void from Function 'checkboxCallback':.
Tried calling: _TaskTileState.checkboxCallback()
Found: _TaskTileState.checkboxCallback(bool) => void

The relevant error-causing widget was: 
  TaskCheckbox TaskCheckbox:file:///F:/Code/Flutter/todoey_flutter/lib/widgets/task_tile.dart:28:17
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:63:5)
#1      _objectNoSuchMethod (dart:core-patch/object_patch.dart:106:9)

The relevant error-causing widget was: 
  TaskCheckbox TaskCheckbox:file:///F:/Code/Flutter/todoey_flutter/lib/widgets/task_tile.dart:28:17
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:63:5)
#1      _objectNoSuchMethod (dart:core-patch/object_patch.dart:106:9)

Here is my code that through this error

    // ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';

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

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

  void checkboxCallback(bool checkboxState) {
    setState(() {
      isChecked = checkboxState;
    });
  }

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

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

I am trying to create a global state to exchange data between two different classes and change state on the screen.

  • this is a task management app where I am getting the data from static variable. I want to exchange boolean between two classes and display the task list according to that boolean.* Can someone help me with this


Solution 1:[1]

import 'package:flutter/material.dart';

class TaskTile extends StatefulWidget {
  @override


 State<TaskTile>  createState() => _TaskTileState();
}
class _TaskTileState extends State<TaskTile> {
  bool isChecked=false;
      void CheckBoxCallback(bool? checkboxstate){
   setState(() {
     isChecked = checkboxstate!;
   });
  }

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title:  Text('baking today',
      style: TextStyle(
          decoration: isChecked?TextDecoration.lineThrough:null),),
      trailing: TaskCheckBox(
        checkboxstate: isChecked,
        togglecheckboxstate: CheckBoxCallback,
        
      ),
    );
  }
}

class TaskCheckBox extends StatelessWidget {
  final bool checkboxstate;
  final Function(bool?)? togglecheckboxstate; // Here is where to make changes, observe the "bool?" i  added.
  TaskCheckBox({required this.checkboxstate,required this.togglecheckboxstate});

  @override
  Widget build(BuildContext context) {
    return Checkbox(
      value: checkboxstate,
      onChanged:togglecheckboxstate, // and here too on your code
    );
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chuks albert