'Is there a way to get a string representation of a variable in a dart class?
In my Flutter app, I have a class named Task as follows:
class Task {
final String id;
final String title;
final String description;
final DateTime date;
bool isDone;
Task({
required this.id,
required this.title,
this.description = "",
required this.date,
this.isDone = false,
});
}
Now I want to map this class to a database table, means for that I need to map every member of the class with a string to create a table. Like this:
"CREATE TABLE tasks (id TEXT PRIMARY KEY, title TEXT, description TEXT)"
Is there a way in Dart to get a String representation of class members, so to avoid declaring additional static const members just for the names of these members, and also to avoid hardcoding these names every place I need them. Thanks.
Solution 1:[1]
You need some reflection, you can get that from the official mirrors library: https://api.dart.dev/stable/2.16.1/dart-mirrors/dart-mirrors-library.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ManuH68 |
