1

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;
2
  • 2
    Note: this code doesn't do any kind of casting. You are only setting a property, and property keys are strings. Commented Nov 17, 2013 at 0:42
  • checkout Maps and hashes in dart Commented Nov 17, 2013 at 0:55

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.