Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Variables name by it's value in Dart Lang

Tags:

dart

dart-html

For Example, I have a variable like this.

var fooBar = 12;

I want a something like this in Dart lang.

print_var_name(fooBar);

which prints:

fooBar

How can I achieve that? Is this even possible?

Thank you.

like image 740
Mr.spShuvo Avatar asked Dec 12 '18 06:12

Mr.spShuvo


1 Answers

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();
  print(some['foo']);
  print(some['bar']); 
}
like image 169
Günter Zöchbauer Avatar answered Sep 28 '22 02:09

Günter Zöchbauer