sm-sayedi
sm-sayedi

Reputation: 935

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.

Upvotes: 0

Views: 567

Answers (1)

Dartist
Dartist

Reputation: 507

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

Upvotes: 1

Related Questions