【问题标题】:Flutter: HTTP Get returns instance of future instead of Image results in ExceptionFlutter:HTTP Get返回未来的实例而不是图像导致异常
【发布时间】:2020-03-04 22:21:06
【问题描述】:

我的目标: 从 URL 下载图片,保存在目录中,然后使用 carousel_slider.dart 包,创建一个滑块。一切运行良好,然后在执行 for 循环后出现运行时错误,提示异常:无法实例化图像编解码器。 使用打印语句后,我得到一个“响应”实例。

根据此错误消息,我可以猜测我的代码正在尝试在下载图像之前对其进行解码。

控制台:

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.
════════════════════════════════════════════════════════════════════════════════════════════════════

我的代码 sn-p:

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');
  }

【问题讨论】:

    标签: flutter async-await


    【解决方案1】:

    好的,问题已解决。网址不正确,因此出现错误。

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多