Home > Back-end >  The argument type 'Function' can't be assigned to the parameter type 'void Funct
The argument type 'Function' can't be assigned to the parameter type 'void Funct

Time:12-14

enter image description here

import 'package:flutter/material.dart';

import '../widgets/main_drawer.dart';

class FiltersScreen extends StatefulWidget {   static const routeName
= '/filtters';

  @override   State<FiltersScreen> createState() =>
_FiltersScreenState(); }

class _FiltersScreenState extends State<FiltersScreen> {   var
_glutenFree = false;   var _vegan = false;   var _vegetarian = false;   var _lactoseFree = false;

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

  @override   Widget build(BuildContext context) {
    return Scaffold(
      drawer: MainDrawer(),
      appBar: AppBar(
        title: Text('Filtters'),
      ),
      body: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.all(20),
            child: Text(
              'Adjust Your Meal Selection!',
              style: Theme.of(context).textTheme.subtitle1,
            ),
          ),
          Expanded(
            child: ListView(
              children: <Widget>[
                _buildSwitchListTile(
                  'Gluten-Free',
                  'Only Include Gluten-Free Meals.',
                  _glutenFree,
                  setState(
                    (newValue) {
                      _glutenFree = newValue;
                    },
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );   } }

i tried to change Function updateValue to final Function(bool newValue) updateValue but i get a proplem in the setState =>> the proplem is (

This expression has a type of 'void' so its value can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void

)

CodePudding user response:

Change the type of the argument updateValue from Function to void Function(bool)?:

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

CodePudding user response:

Here is how to fix it, first as you said, change

Function onChanged,

to this:

void Function(bool) onChanged,

This means that onChanged will have an argument, and will return nothing, so when you pass it, you need to use a lambda function:

_buildSwitchListTile(
  'Gluten-Free',
  'Only Include Gluten-Free Meals.',
  _glutenFree,
  (newValue) => setState(
    () {
      _glutenFree = newValue;
    },
  ),
),
  • Related