我想定义一个带有通用回调的冻结类 [https://pub.dev/packages/freezed]。
冻结类:
import 'package:freezed_annotation/freezed_annotation.dart';
part 'foo.freezed.dart';
@freezed
abstract class Foo<T> with _$Foo {
factory Foo({
// String Function() callBackOne,
String Function(T) callBackTwo,
}) = _Foo;
}
使用 Freezed 类的小部件:
class MyHomePage extends StatelessWidget {
// final fooOne = Foo<int>(callBackOne: () => 'Result: 42');
final fooTwo = Foo<int>(callBackTwo: (value) => 'Result: ${value * 3}');
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(fooTwo.callBackTwo(14)),
),
);
}
}
错误:
lib/foo.freezed.dart:128:26: Error: The return type of the method '_Foo.callBackTwo' is 'String Function(T)', which does not match the return type, 'String Function(dynamic)', of the overridden method, '_$Foo.callBackTwo'.
Change to a subtype of 'String Function(dynamic)'.
String Function(T) get callBackTwo;
^
lib/foo.freezed.dart:31:26: Context: This is the overridden method ('callBackTwo').
String Function(T) get callBackTwo;
^
你知道我的代码有什么问题吗?是冻结的限制吗?你知道解决方法吗?
谢谢!!!