如何在Dart中扩展List?

Set*_*add 22 dart

我想在dart中创建一个更专业的列表.我无法直接扩展List.我有什么选择?

Ale*_*uin 21

要创建一个类实现List,有几种方法:

import 'dart:collection';

class MyCustomList<E> extends ListBase<E> {
  final List<E> l = [];
  MyCustomList();

  void set length(int newLength) { l.length = newLength; }
  int get length => l.length;
  E operator [](int index) => l[index];
  void operator []=(int index, E value) { l[index] = value; }

  // your custom methods
}
Run Code Online (Sandbox Code Playgroud)
import 'dart:collection';

class MyCustomList<E> extends Base with ListMixin<E> {
  final List<E> l = [];
  MyCustomList();

  void set length(int newLength) { l.length = newLength; }
  int get length => l.length;
  E operator [](int index) => l[index];
  void operator []=(int index, E value) { l[index] = value; }

  // your custom methods
}
Run Code Online (Sandbox Code Playgroud)
import 'package:quiver/collection.dart';

class MyCustomList<E> extends DelegatingList<E> {
  final List<E> _l = [];

  List<E> get delegate => _l;

  // your custom methods
}
Run Code Online (Sandbox Code Playgroud)
import 'package:collection/wrappers.dart';

class MyCustomList<E> extends DelegatingList<E> {
  final List<E> _l;

  MyCustomList() : this._(<E>[]);
  MyCustomList._(l) :
    _l = l,
    super(l);

  // your custom methods
}
Run Code Online (Sandbox Code Playgroud)

根据您的代码,每个选项都有其优点.如果您包装/委托现有列表,则应使用最后一个选项.否则,根据您的类型层次结构使用两个第一个选项中的一个(mixin允许扩展其他对象).


Set*_*add 16

dart:collection中有一个ListBase类.如果扩展此类,则只需实现:

  • get length
  • set length
  • []=
  • []

这是一个例子:

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);
}
Run Code Online (Sandbox Code Playgroud)


cre*_*not 11

Dart 2.6 引入了一种扩展类的新方法。
现在,您可以创建extensionList是这样的:

extension MyCustomList<T> on List<T> {
  // Any methods you want can be added here.
}
Run Code Online (Sandbox Code Playgroud)

您添加的方法可以隐式使用,即您可以在任何导入List时使用它们extension
这是功能规范中的一个示例

extension MyFancyList<T> on List<T> {
  int get doubleLength => this.length * 2;
  List<T> operator-() => this.reversed.toList();
  List<List<T>> split(int at) => 
      <List<T>>[this.sublist(0, at), this.sublist(at)];
  List<T> mapToList<R>(R Function(T) convert) => this.map(convert).toList();
}
Run Code Online (Sandbox Code Playgroud)

您可以在 any 上使用这些新成员List,例如:

const list = <String>['some', 'elements'];

list.doubleLength; // Evaluates to 4.
Run Code Online (Sandbox Code Playgroud)


小智 5

这个答案已经过时了,我正在为我自己的项目做这件事,所以我想我会通过发布一个不涉及任何覆盖或实施的非常干净的答案来帮助一些人.

颤动包有一个名为可扩展List类DelegatingList,使得延伸的名单微不足道。

class FruitList extends DelegatingList<Fruit> {
    final List<Fruit> _fruits = [];

    List<Fruit> get delegate => _fruits;

    // custom methods
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助像我一样遇到这个问题的人!