如何侦听dart中对象的贴图属性更改

如何侦听dart中对象的贴图属性更改,dart,properties,Dart,Properties,范例 我想要的是侦听映射属性更改,我应该怎么做?您当前的实现不起作用,因为您真正要做的是使用getget调用访问您的内部映射,然后向其插入值 我不确定界面是否与示例中的相同对您来说很重要,但我做了以下几点修改,稍微改变了您与小马类的交互方式: class Pony { Map _o = {}; Object operator [](Object key) => _o[key]; void operator []=(Object key, Object value) {

范例


我想要的是侦听映射属性更改,我应该怎么做?

您当前的实现不起作用,因为您真正要做的是使用get
get
调用访问您的内部映射,然后向其插入值

我不确定界面是否与示例中的相同对您来说很重要,但我做了以下几点修改,稍微改变了您与
小马
类的交互方式:

class Pony {
  Map _o = {};

  Object operator [](Object key) => _o[key];
  void operator []=(Object key, Object value) {
    _o[key] = value;
    print('Key: "$key" updated with the value "$value"');
  }
}

void main() {
  var p = new Pony();
  p['nothing'] = 'no active';
  // Key: "nothing" updated with the value "no active"

  print('Value from map: ${p['nothing']}');
  // Value from map: no active
}
class Pony {
  Map _o = {};

  Object operator [](Object key) => _o[key];
  void operator []=(Object key, Object value) {
    _o[key] = value;
    print('Key: "$key" updated with the value "$value"');
  }
}

void main() {
  var p = new Pony();
  p['nothing'] = 'no active';
  // Key: "nothing" updated with the value "no active"

  print('Value from map: ${p['nothing']}');
  // Value from map: no active
}