return_ in_ generator
Can't return a value from a generator function that uses the 'async*' or 'sync*' modifier.
Description
#
The analyzer produces this diagnostic when a generator function (one whose
body is marked with either
async*
or
sync*
) uses either a
return
statement to return a value or implicitly returns a value because of using
=>
. In any of these cases, they should use
yield
instead of
return
.
Examples
#
The following code produces this diagnostic because the method
f
is a
generator and is using
return
to return a value:
Iterable<int> f() sync* {
return 3;
}
The following code produces this diagnostic because the function
f
is a
generator and is implicitly returning a value:
Stream<int> f() async* => 3;
Common fixes
#
If the function is using
=>
for the body of the function, then convert it
to a block function body, and use
yield
to return a value:
Stream<int> f() async* {
yield 3;
}
If the method is intended to be a generator, then use
yield
to return a
value:
Iterable<int> f() sync* {
yield 3;
}
If the method isn't intended to be a generator, then remove the modifier
from the body (or use
async
if you're returning a future):
int f() {
return 3;
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.