在Dart中的Polymer元素中使数据类型可迭代

luc*_*ins 4 dart polymer

我在Dart中有一个自定义数据类型,我想使用它进行迭代template repeat.以下是有问题的数据类型的精简版本:

class Note {
  String content;
  Note(this.content);
}

class Notebook {
  List<Note> notes;
  Notebook(this.notes);
}
Run Code Online (Sandbox Code Playgroud)

我希望能够像这样迭代Notes Notebook:

<polymer-element name="x-notebook=view">
  <ul>
    <template repeat="{{note in notebook}}">
      <li is="x-note-view" note="{{note}}></li>
    </template>
  </ul>

  <script ...></script>
</polymer-element>
Run Code Online (Sandbox Code Playgroud)

当然,问题是标准Lists可以通过这种方式迭代,但我不知道如何修改我的自定义Notebook数据类型来做同样的事情.

其中一种方法似乎工作是一个连接toList()方法的Notebook类:

List<Note> toList() => notes;
Run Code Online (Sandbox Code Playgroud)

但我希望在没有首先转换为a的情况下实现这一目标List.

Vya*_*rov 5

阅读polymer_expression包的来源表明in操作符的右侧必须是Iterable,所以你必须实现这个接口.

我做了一个快速测试,以下似乎工作:

import 'dart:collection' show IterableMixin;

// [IterableMixin] implement all methods of [Iterable]
// in terms of [iterator].
class Notebook extends Object with IterableMixin<Note> {
  List<Note> notes;
  Notebook(this.notes);

  get iterator => notes.iterator;
}
Run Code Online (Sandbox Code Playgroud)