Rex
Rex

Reputation: 313

flutter: Too many positional arguments: 0 expected, but 1 found when I am passing the function to other function

A funtion call setfilter(_filter) is been passed to this page called savefilter but I want to return back the value of this function called savefilter(selectfilters)---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------enter image description here

import 'package:dishes_app/widgets/main_drawer.dart';
import 'package:flutter/material.dart';

class FilterScreen extends StatefulWidget {
  static const routeName = '/filters';
  final VoidCallback saveFilters;

  FilterScreen(this.saveFilters);

  @override
  _FilterScreenState createState() => _FilterScreenState();
}

class _FilterScreenState extends State<FilterScreen> {
  bool _glutenFree = false;
  bool _vegetarian = false;
  bool _vegan = false;
  bool _lactoseFree = false;

  Widget buildSwitchtile(String title, String desc, bool currentvalue,
      Function(bool? myBool) updatevalue) {
    return SwitchListTile(
      title: Text(title),
      value: currentvalue,
      subtitle: Text(desc),
      onChanged: updatevalue,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
        actions: [
          IconButton(
              icon: Icon(Icons.save),
              onPressed: () {
                final selectedFilters = {
                  'gluten': _glutenFree,
                  'lactose': _lactoseFree,
                  'vegan': _vegan,
                  'vegetarian': _vegetarian,
                };
              widget.saveFilters(selectedFilters);
              }),
        ],
      ),
      drawer: MainDrawer(),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.all(20.0),
            child: Text(
              'Adjust Your meal selection',
              style: Theme.of(context).textTheme.title,
            ),
          ),
          Expanded(
            child: ListView(
              children: [
                buildSwitchtile('Gluten-Free', 'Only include gluten-free meals',
                    _glutenFree, (newvalue) {
                  setState(() {
                    _glutenFree = newvalue!;
                  });
                }),
                buildSwitchtile(
                    'Lactose-Free', 'Only include Lactose Free', _lactoseFree,
                    (newvalue1) {
                  setState(() {
                    _lactoseFree = newvalue1!;
                  });
                }),
                buildSwitchtile('Vegetarian ', 'Only include Vegetarian food  ',
                    _vegetarian, (newvalue2) {
                  setState(() {
                    _vegetarian = newvalue2!;
                  });
                }),
                buildSwitchtile('Vegan', 'Only include Vegan', _vegan,
                    (newvalue3) {
                  setState(() {
                    _vegan = newvalue3!;
                  });
                })
              ],
            ),
          ),
        ],
      ),
    );
  }
}

Upvotes: 1

Views: 1644

Answers (1)

Bach
Bach

Reputation: 3326

In your property, you are declaring saveFilter as a VoidCallback, hence the error since it doesn't take any argument. To take an argument such as Map, change it to Function(Map), something like:

class FilterScreen extends StatefulWidget {
  static const routeName = '/filters';
  final Function(Map) saveFilters; // Change the type declaration here

  FilterScreen(this.saveFilters);

  @override
  _FilterScreenState createState() => _FilterScreenState();
}

Upvotes: 3

Related Questions