是否可以为Dart扩展添加接口?在Swift中,你可以这样做:
protocol MyProtocol {
associatedtype Item
mutating func nextItem() -> Item?
}
extension MyClass: MyProtocol {
public typealias Item = T
public mutating func nextItem() -> T? {
// ...
}
}
如何用飞镖?这似乎是不可能的:
extension MyClassExtension<T> on MyClass implements MyInterface {
T? nextItem() {
// ...
}
}
不可能向Dart扩展添加接口。查看此处的讨论:
- https://github.com/dart-lang/language/issues/475
- https://github.com/dart-lang/language/issues/736
您必须手动从接口添加方法,就像您将普通扩展方法一样:
extension MyClassExtension<T> on MyClass<T> {
T? nextItem() {
// ...
}
}