Created
September 16, 2021 08:50
-
-
Save Bringoff/ae56b93179d5c6e683913e6ee72e1383 to your computer and use it in GitHub Desktop.
Can't switch on generic lists
This file contains hidden or 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
| void main() { | |
| print(getAttribute<List<String>>(['1', '2'])); | |
| } | |
| T getAttribute<T>(dynamic rawAttribute) { | |
| switch (T) { | |
| case bool: | |
| return rawAttribute ?? false; | |
| case String: | |
| return rawAttribute ?? ''; | |
| case int: | |
| return rawAttribute ?? 0; | |
| case double: | |
| return rawAttribute ?? 0.0; | |
| case List: | |
| print('Catched List of Strings by List'); | |
| } | |
| switch (T.toString()) { | |
| case 'List<String>': | |
| return (rawAttribute as List).cast<String>() as T; | |
| case 'List<int>': | |
| return (rawAttribute as List).cast<int>() as T; | |
| case 'List<double>': | |
| return (rawAttribute as List).cast<double>() as T; | |
| case 'List<bool>': | |
| return (rawAttribute as List).cast<bool>() as T; | |
| } | |
| return rawAttribute; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment