loadImageByProvider static method

Future<Image> loadImageByProvider(
  1. ImageProvider<Object> provider, {
  2. ImageConfiguration config = ImageConfiguration.empty,
})

ImageProvider转ui.Image 实际使用

ui.Image image = await ImageUtils.loadImageByProvider( CachedNetworkImageProvider(imgUrl));

Implementation

static Future<ui.Image> loadImageByProvider(
  ImageProvider provider, {
  ImageConfiguration config = ImageConfiguration.empty,
}) async {
  Completer<ui.Image> completer = Completer<ui.Image>(); //完成的回调
  ImageStreamListener? listener;
  ImageStream stream = provider.resolve(config); //获取图片流
  listener = ImageStreamListener((ImageInfo frame, bool sync) {
    //监听
    final ui.Image image = frame.image;
    completer.complete(image); //完成
    stream.removeListener(listener!); //移除监听
  });
  stream.addListener(listener); //添加监听
  return completer.future; //返回
}