Search code examples
flutterdartcallbackflutter-layoutdart-null-safety

How does the onPressed voidcallback change the boolean in my function?


Situation:

I have a checkbox in one place and i am sending the callback etc. up the widget tree to run a setState and run the function applyFilters().

The NeededChecked is also routed up to the checkbox-value.

Question:

What i am struggling to understand is why this works.

Specifically how the onPressed callback is able to set the value of the bool isNeededState to true/false?

Here is the code that is run. The only important part is the passing of the bool isNeededState to the neededCheked.

 void neededFilterCalled(bool isNeededState) {
    setState(() {
      NeededChecked = isNeededState;
      applyFilters();
    });
}

And here is the checkbox widget:

Widget build(BuildContext context) {
  return Checkbox(
    value: isNeededChecked,
    onChanged: neededFilterCalled,
  );
}

Solution

  • Writing

    onChanged: neededFilterCalled,
    

    is shorthand for

    onChanged: (value) => neededFilterCalled(value),