How do I extend a List in Dart?

dart

Solution

There is a ListBase class in dart:collection. If you extend this class, you only need to implement:

- `get length`

- `set length`

- `[]=`

- `[]`

Here is an example:

import 'dart:collection';

class FancyList<E> extends ListBase<E> {
  List innerList = new List();

  int get length => innerList.length;

  void set length(int length) {
    innerList.length = length;
  }

  void operator[]=(int index, E value) {
    innerList[index] = value;
  }

  E operator [](int index) => innerList[index];

  // Though not strictly necessary, for performance reasons
  // you should implement add and addAll.

  void add(E value) => innerList.add(value);

  void addAll(Iterable<E> all) => innerList.addAll(all);
}

void main() {
  var list = new FancyList();

  list.addAll([1,2,3]);

  print(list.length);
}

Problem

I want to create a more specialized list in dart. I can't directly extend List. What are my options?

Original source

Related problems