Dart Polymer错误'List'不是'value'的'ObservableList'类型的子类型

时间:2015-02-08 13:43:30

标签: dart dart-polymer core-elements

我正在使用core-ajax-dart获取一些数据并将其放入core-list-dart中。我一直收到这个错误。我能够成功传递String的标题,但我无法传递联系人。它失败并出现以下错误

  

例外:未捕获错误:输入'列表'不是类型的子类型   ' ObservableList'价值'。

主页

<core-ajax-dart auto id="_ajax" url="https://polymer-contacts.firebaseio.com/{{category}}.json" handleAs="json"></core-ajax-dart>
<contacts-page class="page" id="contacts" contacts="{{contacts}}" heading="{{heading}}" flex></contacts-page>
List contacts;
ContactsPage cp = $['contacts'] as ContactsPage;
var ajax = $['_ajax'] as CoreAjax;
    ajax.on["core-response"].listen((event) {
      var detail = event.detail;
      var response = detail['response'];
      cp.contacts = response;
    });

元素定义

<div id="title" flex>{{heading}}</div>
<core-list-dart id="list" data="{{contacts}}">
@published List contacts;
@published String heading;

堆栈追踪:

Exception: Uncaught Error: type 'List' is not a subtype of type 'ObservableList' of 'value'.
Stack Trace:
#0      CoreList.data= (package:core_elements/core_list_dart.dart:48:124)
#1      main.<anonymous closure> (http://localhost:8080/index.html_bootstrap.dart:114:27)
#2      GeneratedObjectAccessorService.write (package:smoke/static.dart:114:11)
#3      write (package:smoke/smoke.dart:34:40)
#4      _updateNode (package:polymer/src/instance.dart:1412:16)
#5      _convertAndCheck (package:polymer_expressions/polymer_expressions.dart:302:16)
#6      _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#7      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341)
#8      _DelayedData.perform (dart:async/stream_impl.dart:595)
#9      _StreamImplEvents.handleNext (dart:async/stream_impl.dart:711)
#10     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:671)
#11     _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#12     _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#13     _handleMutation (dart:html:41817)

Almost full source

1 个答案:

答案 0 :(得分:1)

<core-list-dart>的数据属性需要ObservableList而不是List

您可以做的是将字段更改为getter / setter,其中传递的List会自动转换为ObservableList

class Model extends Object with Observable {
// or class SomeElement extends PolymerElement {

  ObservableList _contacts;
  @observable ObservableList get contacts => _contacts;
  set contacts(List contacts) {
    final old = _contacts;
    if(contacts is ObservableList) {
      _contacts = contacts;
    }
    _contacts = toObservable(contacts);
    notifyPropertyChange(#contacts, old, _contacts);
  }
}