【问题标题】:The argument type 'Function' can't be assigned to the parameter type 'void Function(bool)?'参数类型“Function”不能分配给参数类型“void Function(bool)?”
【发布时间】:2022-01-17 05:31:45
【问题描述】:

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;
                    },
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );   } }

我尝试将 Function updateValue 更改为 final Function(bool newValue) updateValue 但我在 setState =>> 中得到一个问题,问题是 (

此表达式的类型为“void”,因此无法使用其值。尝试 检查您是否使用了正确的 API;可能有一个 函数或调用返回你没想到的 void。还要检查类型 也可能为 void 的参数和变量

)

【问题讨论】:

    标签: flutter dart boolean setstate


    【解决方案1】:

    将参数updateValue的类型从Function更改为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,
      );   
    }
    

    【讨论】:

      【解决方案2】:

      这里是如何修复它,首先如你所说,改变

      Function onChanged,
      

      到这里:

      void Function(bool) onChanged,
      

      这意味着onChanged会有一个参数,并且不会返回任何东西,所以当你传递它时,你需要使用一个lambda函数:

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

      【讨论】:

        猜你喜欢
        • 2021-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-01
        • 2023-04-05
        • 1970-01-01
        • 2021-09-01
        • 1970-01-01
        相关资源
        最近更新 更多