[英]Flutter: HTTP Get returns instance of future instead of Image results in Exception
My objective: Download Image from a URL, save it in a directory and then using carousel_slider.dart package, create a slider.我的目标:从 URL 下载图像,将其保存在一个目录中,然后使用carousel_slider.dart包,创建一个滑块。 Everything runs fine then after the for loop is executed I get a runtime error saying Exception: Could not instantiate image codec.
一切运行正常,然后在执行 for 循环后,我收到一个运行时错误,提示异常:无法实例化图像编解码器。 After using a print statement I get an Instance of 'response'.
使用打印语句后,我得到一个“响应”实例。
Based on this error message what I can guess is my code is trying to decode image before it's downloaded.根据此错误消息,我可以猜测我的代码正在尝试在下载图像之前对其进行解码。
Console:安慰:
Performing hot restart...
Syncing files to device sdk google atv x86...
Restarted application in 3,046ms.
I/flutter ( 3664): ::Data connection is available.
I/flutter ( 3664): ::Mosquitto client connecting....
I/flutter ( 3664): ::Subscription confirmed -> topic -> topic/registered
I/flutter ( 3664): ::Change notification:: topic -> <topic/registered>, payload -> <1>
I/flutter ( 3664): Payload: 1
I/flutter ( 3664): ::Subscription confirmed -> topic -> topic/control_instruction
I/flutter ( 3664): Instance of 'Response'
I/flutter ( 3664): Instance of 'Response'
I/flutter ( 3664): Instance of 'Response'
I/flutter ( 3664): [/data/user/0/com.stellar.dnb/app_flutter/Notice/1583231584.jpg, /data/user/0/com.stellar.dnb/app_flutter/Notice/1583237477.jpg, /data/user/0/com.stellar.dnb/app_flutter/Notice/1583237505.jpg]
I/flutter ( 3664): ::Mosquitto client connected
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following _Exception was thrown resolving an image codec:
Exception: Could not instantiate image codec.
When the exception was thrown, this was the stack:
#0 _futurize (dart:ui/painting.dart:4304:5)
#1 instantiateImageCodec (dart:ui/painting.dart:1682:10)
#2 PaintingBinding.instantiateImageCodec (package:flutter/src/painting/binding.dart:88:12)
#3 FileImage._loadAsync (package:flutter/src/painting/image_provider.dart:653:24)
<asynchronous suspension>
...
Path: /data/user/0/com.stellar.dnb/app_flutter/Notice/1583231584.jpg
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (2) Exception caught by image resource service ════════════════════════════════════════════
Exception: Could not instantiate image codec.
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (3) Exception caught by image resource service ════════════════════════════════════════════
Exception: Could not instantiate image codec.
════════════════════════════════════════════════════════════════════════════════════════════════════
My code snippet:我的代码片段:
getAssetList() async {
response =
await http.post(_noticeURL, body: parseJson.convertToJson(deviceID));
if (response.statusCode == 200) {
parseJson.decodeJson(response.body.toString());
await downloadAsset(addNoticeToList());
} else
print('HTTP Response StatusCode: ${response.statusCode}');
}
addNoticeToList() {
List _assetList = [];
if (parseJson.responseJsonDecoded.isNotEmpty) {
for (dynamic value in parseJson.responseJsonDecoded.values) {
for (dynamic notice in value) {
_assetList.add(notice);
}
}
return _assetList;
}
}
downloadAsset(List assetList) async {
//imgList.clear();
String _dirPath = await dirManager.createFolderInAppDocDir('Notice');
for (String _asset in assetList) {
var assetResponse = await http.get(_noticeDownloadURL + _asset);
var _filePathAndName = (p.join(_dirPath, _asset));
imgList.add(_filePathAndName);
File imageFile = File(_filePathAndName);
print(assetResponse);
imageFile.writeAsBytesSync(assetResponse.bodyBytes);
}
print('$imgList');
}
Okay the issue is resolved.好的问题解决了。 The url was incorrect, hence the error.
网址不正确,因此出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.