问题描述
我在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()
);
解决方法
您的代码有两个主要问题:
- 您应命名函数的输入,例如
notifier
之类的名称。
typedef ContextConditionFilter<T,V> = V Function(T notifier);
- 在定义函数时使用
return
时不需要=>
:
class MyStateNotifier extends StateNotifier<DrawMenuState> {
bool get myValue => true;
}
如果您解决了这些问题,那么其他所有方法都应该起作用。
您也不需要在通知程序函数周围加括号。
ContextStateWidget<MyStateNotifier>(
filter: (MyStateNotifier notifier) => notifier.myValue,child: const OtherWidget(),);
,
感谢aligator的回复,但这实际上是分析仪问题。重新启动VSCode后,问题消失了:-)