Get Variables name by it's value in Dart Lang


For Example, I have a variable like this.

var fooBar = 12;

I want a something like this in Dart lang.


which prints:


How can I achieve that? Is this even possible?

Thank you.


There is no such thing in Dart for the web or for Flutter.
Reflection can do that, but reflection is only supported in the server VM because it hurts tree-shaking.

You need to write code for that manually or use code generation where you need that.

An example

class SomeClass {
  String foo = 'abc';
  int bar = 12;

  dynamic operator [](String name) {
    switch(name) {
      case 'foo': return foo;
      case 'bar': return bar;
      default: throw 'no such property: "$name";

main() {
  var some = SomeClass();

Answered By – Günter Zöchbauer

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *