I am trying to set expanded height according to image from URL.
I implemented a Future function.
Future<double> _calculateImageRatio(String url) async {
double ratioX = 0.0;
Completer<Size> completer = Completer();
Image image = Image.network(url);
image.image.resolve(ImageConfiguration()).addListener(
ImageStreamListener(
(ImageInfo image, bool synchronousCall) {
var myImage = image.image;
Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
completer.complete(size);
ratioX = size.height / size.width;
},
),
);
return ratioX;
}
Although this function returns double value, I can not run the code as it gives
The argument type 'num' can't be assigned to the parameter type 'double?'.
error.
in SliverAppBar.
SliverAppBar _customAppBar(BuildContext context) {
return SliverAppBar(
expandedHeight: 150 * _calculateImageRatio("url-of-image"),//This line is problematic, the error occurs in this line.
...
...
...
);
}
How can I solve my problem? I looked FutureBuilder but I can not solved my problem, I could not understand how can I implement FutureBuilder here, or any other solution?