Tatsuhiko Mizuno
Tatsuhiko Mizuno

Reputation: 457

How to use bracket notation in Dart object?

in javascript,we can get array length by ArrayName["length"] instead of ArrayName.length. but it not work in Dart... I want get object properties by string. Is there a way to do this in Dart without using Map?

Upvotes: 0

Views: 678

Answers (1)

julemand101
julemand101

Reputation: 31219

This is not possible in Dart. Dart is a compiled language and makes optimizations based on what methods are actually used in e.g. classes. So by allowing accessing fields and methods by random strings, it would not be possible to predict what fields/methods are actually used and therefore it needs to provide access to every field/method available which would increase the size of the compiled application by a lot.

If you are running directly on the Dart VM, you can use dart:mirrors to provide reflection on your objects, but that is only possible since you are running the source code directly with the Dart VM, and it can therefore reflect over the source.

But after a Dart program is compiled, the compiler will have removed the parts of the source code which are never going to be used.

Upvotes: 1

Related Questions