向 Dart 扩展方法添加接口

Adding an interface to a Dart extention method

是否可以为 Dart 扩展添加接口?在 Swift 你可以这样做:

protocol MyProtocol {
  associatedtype Item
  mutating func nextItem() -> Item?
}

extension MyClass: MyProtocol {

  public typealias Item = T

  public mutating func nextItem() -> T? {
    // ...
  }
}

你如何在 Dart 中做到这一点?这似乎是不可能的:

extension MyClassExtension<T> on MyClass implements MyInterface {
  T? nextItem() {
    // ...
  }
}

无法将接口添加到 Dart 扩展。请参阅此处的讨论:

您必须像添加普通扩展方法一样从界面手动添加方法:

extension MyClassExtension<T> on MyClass<T> {
  T? nextItem() {
    // ...
  }
}