Home > Back-end >  How do I create an anonymous function that doesn't generate a void function problem in Flutter
How do I create an anonymous function that doesn't generate a void function problem in Flutter

Time:12-07

How do I create an inline function to fix this error: The argument type 'void Function()' can't be assigned to the parameter type 'void Function.

Consider the following code:

                Switch(value: _showChart, onChanged: () {   //this generates the error
                  setState(() {
                    _showChart = value;
                  });
                },)

If I extracted it to a function it would look something like this, but I don't want to do that:

  VoidCallback? onSwitch(val){
    setState(() {
      _showChart = val;
    });
  }

Is there a way to do that inline?

CodePudding user response:

onChanged: contains a value parameter.

You are missing a typo, the value parameter. It will be,

Switch(
  value: true,
  onChanged: (value) {
    setState(() {
     _showChart = value;
      });
  },
),
  • Related