【问题标题】:error: The argument type ‘..’ can't be assigned to the parameter type ‘..’错误:参数类型“..”不能分配给参数类型“..”
【发布时间】:2019-10-15 11:21:52
【问题描述】:

我正在尝试调用一个接受另一个函数作为参数的函数。

当我尝试使用泛型进行概括时,我收到以下错误

错误:不能将参数类型“..”分配给参数类型“..”。

class MyType {}
typedef MyFunctionType = void Function(Function <MyValue extends MyType> (MyValue?, MyValue));

class MyClass <MyValue extends MyType> {

  late MyFunctionType myFunc;

  MyClass(this.myFunc) {
    (observer) => this.newValues(observer.oldValue,observer.oldValue);
  }


  MyClass <MyValue> _select(MyValue Function(MyValue) selector)  {
    var observerFunc = (oldValue,newValue) {

    };
    return MyClass((observer) => this.observe(observerFunc));
  }


  void observe(MyFunctionType func){
    this.myFunc = func;
  }

  void newValues(MyValue oldValue, MyValue newValue) {
    // TODO Add functionality
  }
}

以下是错误

error: The argument type 'void Function(dynamic Function<MyValue₀ extends MyType>(MyValue, MyValue))' can't be assigned to the parameter type 'void Function(dynamic Function<MyValue extends MyType>(MyValue, MyValue))'. (argument_type_not_assignable at [dartTry] test/Params2Arguments.dart:16)

【问题讨论】:

    标签: dart


    【解决方案1】:

    我也有类似的问题,不过我的例子要简单得多:

    class Answer extends StatelessWidget {
      final Function selectHandler;
    
      Answer(this.selectHandler);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: RaisedButton(
            ...
            onPressed: selectHandler,
          ),
        );
      }
    }
    

    分配onPressed 函数的行的错误是:

    参数类型“Function”不能分配给参数类型“void Function()”。 飞镖(argument_type_not_assignable)

    将 void 类型添加到我的函数属性中就可以了,但这需要添加括号:

    class Answer extends StatelessWidget {
      final void Function() selectHandler;
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2021-10-03
      • 2020-01-05
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2023-01-23
      • 2021-01-05
      相关资源
      最近更新 更多