为什么我会收到这个异常?
════════ Exception caught by image resource service ════════════════════════════
The following StateError was thrown by an image listener:
Bad state: Future already completed
When the exception was thrown, this was the stack
#1 _xxxState.setup.<anonymous closure>
xxx/ui/xxx.dart:209
#2 ImageStreamCompleter.setImage
package:flutter/…/painting/image_stream.dart:633
#3 MultiImageStreamCompleter._emitFrame
package:cached_network_image/…/image_provider/multi_image_stream_completer.dart:156
#4 MultiImageStreamCompleter._decodeNextFrameAndSchedule
package:cached_network_image/…/image_provider/multi_image_stream_completer.dart:141
<asynchronous suspension>
(elided one frame from dart:async)
代码
initState(){
_refresh();
}
_refresh() async {
...
await setUp();
}
setup() async {
var data = await _bloc.selectFloorPlan(floorPlanId);
Image image = Image(image: CachedNetworkImageProvider(data));
Completer<ui.Image> completer = Completer<ui.Image>();
image.image
.resolve(ImageConfiguration())
.addListener(ImageStreamListener((ImageInfo image, bool _) {
completer.complete(image.image);
if (completer.isCompleted) {
if (mounted) {
floorPlanAnnotationUtil.updateValues(
containerHeight: containerFixedHeight,
containerWidth: MediaQuery.of(context).size.width -
20, // minus the container padding 10 * 2
imageHeight:
DevicePixelUtil.toDevicePixel(context, image.image.height),
imageWidth:
DevicePixelUtil.toDevicePixel(context, image.image.width),
);
}
// setState(() {});
}
}));
}
错误指向这条线
completer.complete(image.image);
看起来您正在使用https://pub.dev/packages/cached_network_image,内部使用https://pub.dev/packages/flutter_cache_manager.默认的缓存管理器可以将多个(相关的(事件添加到流中。例如,如果缓存中的图像过时,则它首先发出缓存的图像,然后再次发出新下载的图像。你可能在这里看到了效果,因为你的Completer
只能完成一次。