跳到主内容

await_of_incompatible_type

“await”表达式不能用于扩展类型不是“Future”子类型的表达式。

描述

#

await 表达式中的表达式类型是扩展类型,并且该扩展类型不是 Future 的子类时,分析器会产生此诊断。

示例

#

以下代码会产生此诊断,因为扩展类型 E 不是 Future 的子类

dart
extension type E(int i) {}

void f(E e) async {
  await e;
}

常见修正

#

如果扩展类型定义正确,则移除 await

dart
extension type E(int i) {}

void f(E e) {
  e;
}

如果该扩展类型 intended to be awaitable(意图是可 await 的),则在 implements 子句中添加 Future (或其子类型)(如果尚无 implements 子句则添加一个),并使表示类型匹配。

dart
extension type E(Future<int> i) implements Future<int> {}

void f(E e) async {
  await e;
}