yield_of_invalid_type
A yielded value of type '{0}' must be assignable to '{1}'.
The type '{0}' implied by the 'yield*' expression must be assignable to '{1}'.
Description
#The analyzer produces this diagnostic when the type of object produced by
a yield
or yield*
expression doesn't match the type of objects that
are to be returned from the Iterable
or Stream
types that are returned
from a generator (a function or method marked with either sync*
or
async*
).
Example
#The following code produces this diagnostic because the getter zero
is
declared to return an Iterable
that returns integers, but the yield
is
returning a string from the iterable:
Iterable<int> get zero sync* {
yield '0';
}
Common fixes
#If the return type of the function is correct, then fix the expression
following the keyword yield
to return the correct type:
Iterable<int> get zero sync* {
yield 0;
}
If the expression following the yield
is correct, then change the return
type of the function to allow it:
Iterable<String> get zero sync* {
yield '0';
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。