Jaimin Modi
Jaimin Modi

Reputation: 1677

Flutter : Closure call with mismatched arguments: function

I have created below builder method to build my widget dynamically :

    Widget _buildSwitchListTile(String title, String description,
      var currentValue, Function updateValue) {
    return SwitchListTile(
      title: Text(title),
      value: currentValue,
      subtitle: Text(description),
      onChanged: updateValue,
    );
  }

You can see above updateValue

Calling it as below :

 _buildSwitchListTile(
              'Gluten-free',
              'Only include gluten-free meals.',
              _glutenFree,
              (newValue) {
                setState(
                  () {
                    _glutenFree = newValue;
                  },
                );
              },
            )

Issue is I am getting Compile time error (red mark) near updateValue in _buildSwitchListTile method.

It's Saying :

The argument type 'Function' can't be assign to the parameter type 'void Function (bool)?'.

That's Issue no.1

Below is the Runtime issue : If I add (paranthesis) as updateValue() then error is gone but am getting runtime error as below :

Closure call with mismatched arguments: function '_FiltersScreenState.build.<anonymous closure>'
    Receiver: Closure: (dynamic) => Null
    Tried calling: _FiltersScreenState.build.<anonymous closure>()
    Found: _FiltersScreenState.build.<anonymous closure>(dynamic) => Null

Below is the pic for reference :

enter image description here

What will be the possible solution I should have? Thanks.

Upvotes: 0

Views: 1640

Answers (1)

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63679

You can change callback function like unction(bool)? updateValue or unction(bool)? updateValue because the onChanged is void Function(T value);

 Widget _buildSwitchListTile(String title, String description,
      var currentValue, Function(bool) updateValue) {

Upvotes: 1

Related Questions