sdk
sdk copied to clipboard
[analyzer] `Future.value` gets incorrectly inferred as non-nullable which leads to a runtime exception
Hi, not sure if this is by design, but:
void main() {
final asset = _ImageAsset.fromImage(null);
print(asset.retrieveAsync());
}
class _ImageAsset {
_ImageAsset.fromImage(Image? image) : _image = image;
Image? _image;
Future<Image>? _future;
Future<Image> retrieveAsync() => _future ?? Future.value(_image); // <-- this line
}
class Image {}
the inferred type of Future.value(_image) is Image even though _image is nullable and it leads to a runtime exception when null is passed ("Uncaught Error, error: Error: TypeError: null: type 'Null' is not a subtype of type 'Image'", tested on dartpad).
Changing Future<Image> retrieveAsync() => _future ?? Future.value(_image); to Future<Image?> retrieveAsync() => _future ?? Future<Image?>.value(_image); works as expected.