For example, I have this function parameter:
class MyClass {
U Function<T, U>(T data) callback;
MyClass ({ this.callback }) : super();
}
var int Function(String value) func = (String value) => int.parse(value);
MyClass(callback: func); // error
The error is:
The argument type 'int Function(String)' can't be assigned to the
parameter type 'U Function<T, U>(T)'.
How can I make this work?
EDIT:
Based on Shubhamhackz's answer, I conclude that the only thing wrong with my code is that because the generics are on the variables and not on the function parameters, and the variables are created when the class is declared and instantiated. I should put the <T, U> on the class declaration itself, and not on the function variable declaration. So the class declaration becomes like this:
class MyClass<T, U> {
U Function(T data) callback;
MyClass ({ this.callback }) : super();
}
question from:
https://stackoverflow.com/questions/65896146/how-to-pass-a-function-onto-a-generic-function-parameter-in-flutter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…