Flutter / Dart:参数类型'bool FunctionMyStateNotifier'不能分配给参数类型'dynamic Functiondynamic'

问题描述

我在Dart中有一个这样的类声明,但是在使用它时,编译器会报告错误

c(5,4,2)

继承了typedef ContextConditionFilter<T,bool> = bool Function(T); class ContextStateWidget<T extends StateNotifier> extends StatefulWidget { final Widget child; final ContextConditionFilter<T,bool> filter; const ContextStateWidget({Key key,@required this.child,@required this.filter}) : super(key: key); }

的类
StateNotifier

使用

class MyStateNotifier extends StateNotifier<DrawMenuState>
{
...
bool get myValue => return true;
}

错误

ContextStateWidget<MyStateNotifier>(
    filter: ((MyStateNotifier notifier) => notifier.myValue),// error here
    child: const OtherWidget()
);

解决方法

您的代码有两个主要问题:

  1. 您应命名函数的输入,例如notifier之类的名称。
typedef ContextConditionFilter<T,V> = V Function(T notifier);
  1. 在定义函数时使用return时不需要=>
class MyStateNotifier extends StateNotifier<DrawMenuState> {
   bool get myValue => true;
}

如果您解决了这些问题,那么其他所有方法都应该起作用。

您也不需要在通知程序函数周围加括号。

ContextStateWidget<MyStateNotifier>(
    filter: (MyStateNotifier notifier) => notifier.myValue,child: const OtherWidget(),);
,

感谢aligator的回复,但这实际上是分析仪问题。重新启动VSCode后,问题消失了:-)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...