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:
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
:
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*
:
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:
void f(list) {
for (var e in list) {
print(e);
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.