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