سؤال

In javascript you can do something like this:

var name = 'myVariableName';
myObject[name] = someValue;

In Dart, can I get variables from class objects by their name as a string? For example

var vector = new Vector3();
vector['x'] = 0.0;
هل كانت مفيدة؟

المحلول

If you want to access normal fields of an arbitrary object through a string you need to use mirrors (reflection). You need to be careful, though, since mirrors can increase the code-size of the dart2js output considerably if not done carefully.

If you just need a string-value store, then a Map is what you are looking for.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top