loadImage function
Asynchronously loads an image from an ImageProvider. Returns a ui.Image that can be used for drawing, or null if no image provider is given.
Implementation
Future<ui.Image?> loadImage(ImageProvider? provider) async {
if (provider == null) return null;
final completer = Completer<ui.Image>();
final imageStream = provider.resolve(ImageConfiguration.empty);
final listener = ImageStreamListener(
(ImageInfo info, bool synchronousCall) {
completer.complete(info.image); // Completes with the loaded image
},
onError: (exception, stackTrace) {
throw exception; // Propagate errors
},
);
imageStream.addListener(listener); // Start loading the image
return completer.future; // Return a future that completes when the image is loaded
}