【问题标题】:Flutter : Closure call with mismatched arguments: functionFlutter:参数不匹配的闭包调用:函数
【发布时间】:2022-08-13 20:33:08
【问题描述】:

我创建了以下构建器方法来动态构建我的小部件:

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

你可以在上面看到更新值

如下调用它:

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

问题是我在附近遇到编译时错误(红色标记)更新值在 _buildSwitchListTile 方法中。

它在说:

参数类型 \'Function\' 不能分配给参数类型 \'无效函数(布尔)?\'。

那是第 1 期

以下是运行时问题: 如果我将 (paranthesis) 添加为 updateValue() 则错误消失但运行时错误如下:

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

下面是图片供参考:

我应该有什么可能的解决方案?谢谢。

    标签: flutter dart flutter-layout dart-null-safety


    【解决方案1】:

    您可以更改回调函数,例如 unction(bool)? updateValueunction(bool)? updateValue 因为 onChangedvoid Function(T value);

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

    【讨论】:

      猜你喜欢
      • 2022-12-21
      • 2013-12-01
      • 1970-01-01
      • 2021-12-09
      • 2021-12-10
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多