简体   繁体   English

Flutter:HTTP Get 返回未来的实例而不是图像导致异常

[英]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.

相关问题 Flutter返回“未来实例” <int> ”而不是期望值 - Flutter returns “Instance of Future<int>” instead of expected value 函数返回 Future 的一个实例<String>而不是字符串 - Function returns an instance of Future<String> instead of String Function 返回“未来实例”<dynamic> ' 在 flutter</dynamic> - Function returns 'Instance of Future<dynamic>' in flutter Flutter 从文件读取返回 Future 的实例<string>而不是文件中的真实文本</string> - Flutter reading from file returns Instance of Future<String> instead of the real text in the file Flutter - '未来的实例<double> ' 而不是数字</double> - Flutter - Instance of 'Future<double>' instead number Dartz,颤振测试 - 无法获得未来的正确部分<Either<L, R> &gt;,而不是那个 - Future 的实例<Either<L, R> &gt; 作为实际结果 - Dartz, flutter test - Can't get a Right part of Future<Either<L, R>>, instead of that - instance of Future<Either<L, R>> as a actual result 如何从 Flutter 中的 Future 实例获取 object? - How to get an object from an instance of Future in Flutter? '未来的实例<string?> ' 而不是字符串数据颤动/火力</string?> - Instance of 'Future<String?>' instead of String data flutter/firebase Flutter Future 返回未来的实例 - Flutter Future returning instance of future 不断返回“未来的实例”<dynamic> &quot; 而不是 String (Flutter) - Keep returning "Instance of 'Future<dynamic>" instead of String (Flutter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM