Dart: Using observable with multiple getter/setter(s)

前端 未结 3 514
误落风尘
误落风尘 2021-01-05 19:05

for my Polymer application I need one observable in two different flavors, for example as an integer-value and and string-value. I use getters and setters to encapsulate the

3条回答
  •  孤独总比滥情好
    2021-01-05 19:38

    I have not tested it, but it should work and I think its notable less code if you have more such properties.

    @CustomTag('click-counter')
    class ClickCounter extends PolymerElement {
      int _count;
      @observable int get count => _count;
      set count(int val) { 
        notifyPropertyChange(#count,_count,val);
        notifyPropertyChange(#strcount, _count.toString(), val.toString());
        _count = val;
    
      @observable String get strcount { 
        // print("TOSTRING "+_count.toString()); 
        return _count.toString();}
    
      set strcount(String val) { 
        count = int.parse(val); // set the new value using the setter not the field to fire property change
      }
    
      ClickCounter.created() : super.created();
    
      void increment() {
        count++;
      }
    }
    

提交回复
热议问题