generics - 通用 TypeDef 不适用于自定义类
问题描述
我想创建一个泛型但带有一些在类外部具有外部的class(Test< T, F>)
变量( ) 。分配运行良好,但正如屏幕截图所示失败。_cellBuilder
typedef
type(CellBuilder)
testA
String
testB
当我从中删除<Product>
时testB
,我得到一个不同的错误。
type '(Product) => Widget is not a subtype of type (dynamic) => Widget
相当混乱。请问有什么帮助吗?
void main() {
testA<String>(String content) {
print(content);
}
Widget testB<Product>(Product item) {
print(item.a);
}
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
Test<int, Product> c = Test(testA, testB);
c.eval();
});
}
class Product {
final String a;
Product(this.a);
}
typedef CellBuilder<F> = Widget Function(F item);
typedef testString<T>(T text);
class Test<T, F> {
final testString _f;
final CellBuilder _cellBuilder;
Test(testString this._f, CellBuilder this._cellBuilder);
eval() {
_f("hello");
_cellBuilder(Product("world"));
}
}
解决方案
应该:
Widget testB(Product item)
不是通用函数:
Widget testB<Product>(Product item)
Product
是一个通用标识符,而不是您期望的参数类型。
如果您以自定义方式编写:
Widget testB<T>(T item)
该错误现在应该是有意义的。
推荐阅读
- inno-setup - Inno Setup 正在扩展进度样式
- python - Django:如何破坏关系
- python - 调用python函数
- flutter - NoSuchMethodError:在 null 上调用了方法“add”——尝试调用:add(“FittedBox”的实例)
- java - JPackage(孵化器)和SQLite数据库问题
- r - 使用 R Separate_Rows 不适用于“|”
- c# - 在同一 Visual Studio 表单中而不是在新窗口中打开链接 (C#)
- r - 在 R 中为分类/连续变量操作 ggplor2
- python - 推送拒绝了 Heroku Python Flask 要求失败 exiv2==0.3.1
- computer-vision - HALCON min_mas_grey()