Skip to content

Instantly share code, notes, and snippets.

@hotdang-ca
Last active August 16, 2022 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotdang-ca/2be26fa3dbd079361a5a82a1d1e2f9ee to your computer and use it in GitHub Desktop.
Save hotdang-ca/2be26fa3dbd079361a5a82a1d1e2f9ee to your computer and use it in GitHub Desktop.
Exploring Dart Mirrors 1
import "dart:mirrors";
class Product {
String type;
String brand;
String model;
Product(this.type, this.brand, this.model);
}
void main() {
final p = new Product("phone", "apple", "iphone");
final m = reflect(p);
late Symbol sortByField;
List<String> sortBy = ['brand', 'type', 'somethingElse', 'model'];
for (var field in sortBy) {
try {
sortByField = new Symbol(field);
print('value of .$field: ${m.getField(sortByField).reflectee}');
} on NoSuchMethodError catch (_) {
print("No such field: $field");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment