13

I try to do the following:

var properties = ["height" , "width"];
for (var prop in properties){
  div.style[prop] = otherdiv.style[prop];
}

But dart doesn't seem to accept this bracket notation, is there any other way to access a property using a string in dart ?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
90intuition
  • 966
  • 3
  • 12
  • 25

1 Answers1

7

You can use the getPropertyValue and setProperty methods like

div.style.setProperty(prop, otherdiv.style.getPropertyValue(prop));
lrn
  • 64,680
  • 7
  • 105
  • 121
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks, only I think it must be `getPropertyValue` instead of `getProperty`. – 90intuition Feb 01 '15 at 12:59
  • 14
    This only works for DOM objects. For general Dart objects, there is no way to access properties from a computed name. That's a deliberate language choice - it makes it possible to optimize object layout differently when it doesn't have to allow unpredictable access. If you really need to dynamically access properties by name (or, preferably, by Symbol), you need to use the Mirror library to do the reflection. – lrn Feb 02 '15 at 10:12
  • You are right, this is only for DOM elements from `dart:html`. There is no way for normal Dart classes except reflection (server/command-line only) or code generation. I'd suggest json_serializable or built_value for that. – Günter Zöchbauer Apr 12 '19 at 18:08