Skip to content

Instantly share code, notes, and snippets.

@Bringoff
Created September 16, 2021 08:50
Show Gist options
  • Select an option

  • Save Bringoff/ae56b93179d5c6e683913e6ee72e1383 to your computer and use it in GitHub Desktop.

Select an option

Save Bringoff/ae56b93179d5c6e683913e6ee72e1383 to your computer and use it in GitHub Desktop.
Can't switch on generic lists
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