它看起来像是飞镖式系统中的一个缺陷。我也鼓励过类似的事情。我不知道一个干净的解决办法。您可以指定一个函数,而不是直接函数,而是一个封装到带有“强”方法签名的类中的函数。类似的方法应该会奏效:
@freezed
abstract class Foo<T> with _$Foo {
factory Foo({
Func<T> callBackTwo,
}) = _Foo;
}
class Func<T> {
final String Function(T) _apply;
Func(this._apply) : assert(_apply != null);
String call(T value) {
return _apply(value);
}
}
class MyHomePage extends StatelessWidget {
final fooTwo = Foo<int>(Func<int>((value) => 'Result: ${value * 3}'));
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(fooTwo.callBackTwo(14)),
),
);
}
}
这不太好,因为你得多打字。但我们可以尽量减少打字:
@freezed
abstract class Foo<T> with _$Foo {
factory Foo({
Func<T> callBackTwo,
}) = _Foo;
factory Foo.from(String Function(T) arg) {
return Foo<T>(callBackTwo: Func<T>(arg));
}
}
class MyHomePage extends StatelessWidget {
final fooTwo = Foo<int>.from((value) => 'Result: ${value * 3}');
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(fooTwo.callBackTwo(14)),
),
);
}
}