Skip to content

Instantly share code, notes, and snippets.

@matanshukry
Created September 9, 2023 11:31
Show Gist options
  • Save matanshukry/f1d466d65563d3b053a2ccc8e9e27f7c to your computer and use it in GitHub Desktop.
Save matanshukry/f1d466d65563d3b053a2ccc8e9e27f7c to your computer and use it in GitHub Desktop.
typedef GenMethod<T> = String Function(T value);
class CustomValue {
final String data1;
final String data2;
CustomValue(this.data1, this.data2);
}
GenMethod<int> getNumToString() {
return (value) => value.toString();
}
GenMethod<CustomValue> getCustomValueToString() {
return (value) => '${value.data1} ${value.data2}';
}
class MyData<T> {
final GenMethod<T> method;
MyData(this.method);
}
String dataToString<T>(MyData<T> anyData, T value) {
return anyData.method(value);
}
void main() {
final datas = [
MyData<int>(getNumToString()),
MyData<CustomValue>(getCustomValueToString()),
];
print(datas.runtimeType);
final data = datas[0];
print(data.runtimeType);
final value = dataToString(data, 2);
print(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment