Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

the problem is the header . Im trying to build the todoye app and as you can see the call back is considered ValueChanged<bool?> not a simple function so i cant move my logic to a another statefullwidget to reduce the rebuilds in my app i tried different things but i get error evertime please help . here is my code :

import 'package:flutter/material.dart';

class TaskTile extends StatefulWidget {
  @override
  _TaskTileState 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(
        'finish up angla\'s course today',
        style: TextStyle(
          decoration: isChecked ? TextDecoration.lineThrough : null,
        ),
      ),
      trailing: TaskCheckBox(
        checkBoxState: isChecked,
        toggleCheckBoxState: checkBoxCallBack,
      ),
    );
  }
}

class TaskCheckBox extends StatelessWidget {
  final bool checkBoxState;
  final ValueChanged<bool?> toggleCheckBoxState;

  const TaskCheckBox({
    required this.checkBoxState,
    required this.toggleCheckBoxState,
  });

  @override
  Widget build(BuildContext context) {
    return Checkbox(
      activeColor: Colors.lime,
      value: checkBoxState,
      onChanged: toggleCheckBoxState,
    );
  }
}

the error is on line 27 ...where i want to pass my custom Function to varaible of my statelessWidget.

like image 201
delaram Avatar asked Apr 14 '26 08:04

delaram


1 Answers

I hit the same issue and eventually I got this working

import 'package:flutter/material.dart';

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

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

  void checkboxCallBack(bool? checkboxState) {
    setState(() {
      isChecked = checkboxState ?? true;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListTile(
      title: Text(
        'Task 1',
        style: TextStyle(
            decoration:
                isChecked ? TextDecoration.lineThrough : TextDecoration.none),
      ),
      trailing: TaskCheckbox(isChecked, checkboxCallBack),
    );
  }
}

class TaskCheckbox extends StatelessWidget {
  final bool checkboxState;
  final Function(bool?) toggleCheckboxState;

  TaskCheckbox(this.checkboxState, this.toggleCheckboxState);

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

Changing this function like this

void checkboxCallBack(bool? checkboxState) {
    setState(() {
      isChecked = checkboxState ?? true;
    });
  }

and toggleCheckbox variable to:

final Function(bool?) toggleCheckboxState;
like image 94
speksy Avatar answered Apr 16 '26 22:04

speksy