Created
September 9, 2023 10:04
-
-
Save matanshukry/843ea65a2b6977d1aaba32b68da5b1ab to your computer and use it in GitHub Desktop.
dart generic method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
void main() { | |
final datas = [ | |
MyData<int>(getNumToString()), | |
MyData<CustomValue>(getCustomValueToString()), | |
]; | |
print(datas.runtimeType); | |
final data = datas[0]; | |
print(data.runtimeType); | |
final value = data.method(2); | |
print(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment