Last active
August 16, 2022 17:30
-
-
Save hotdang-ca/2be26fa3dbd079361a5a82a1d1e2f9ee to your computer and use it in GitHub Desktop.
Exploring Dart Mirrors 1
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
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