您能看到以下泛型有什么问题吗?
您可以在此处查看和修改此代码:https://dartpad.dartlang.org/c417f62f2134a7ab828b36ba70564a8d
// import 'dart:async';
typedef void PrintFunction<T>(String p1, T p2);
class ClassA<String, T> {
String str;
T myprinter;
ClassA({this.str, this.myprinter});
}
class ClassB {
String str;
}
main() {
var smallClass = ClassB();
var hello = "input string";
PrintFunction<ClassB> mp = (hello, smallClass)=> print('Hello from PrintFunction');
ClassA s = ClassA(
str: "",
myprinter: mp
);
s.myprinter();
}
分析器错误,很奇怪:
The value of the local variable 'smallClass' isn't used.
The value of the local variable 'hello' isn't used.
答案 0 :(得分:1)
这可能有效
var s = MyClasse<String>(
t1: "",
myprinter: p);
您还应该注意,
class MyClasse<T1, MyPrinter>
MyPrinter
引入了一个泛型类型变量,它没有引用typedef void MyPrinter