async_for_in_wrong_context
The async for-in loop can only be used in an async function.
Description
#The analyzer produces this diagnostic when an async for-in loop is found in
a function or method whose body isn't marked as being either async
or
async*
.
Example
#The following code produces this diagnostic because the body of f
isn't
marked as being either async
or async*
, but f
contains an async
for-in loop:
dart
void f(list) {
await for (var e in list) {
print(e);
}
}
Common fixes
#If the function should return a Future
, then mark the body with async
:
dart
Future<void> f(list) async {
await for (var e in list) {
print(e);
}
}
If the function should return a Stream
of values, then mark the body with
async*
:
dart
Stream<void> f(list) async* {
await for (var e in list) {
print(e);
}
}
If the function should be synchronous, then remove the await
before the
loop:
dart
void f(list) {
for (var e in list) {
print(e);
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。