문제

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