Dirk
Dirk

Reputation: 3221

How do I create an anonymous function that doesn't generate a void function problem in Flutter

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?

Upvotes: 0

Views: 199

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63614

onChanged: contains a value parameter.

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

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

Upvotes: 3

Related Questions