【问题标题】:Exception: Could not instantiate image codec in Flutter异常:无法在 Flutter 中实例化图像编解码器
【发布时间】:2020-06-27 21:14:52
【问题描述】:

我正在开发一个小应用程序,该应用程序从 Internet 获取图像并使用 NetworkImage() 小部件将其显示在屏幕上。我的代码如下所示:

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text("I am Rich"),
        backgroundColor: Colors.blueGrey[900],
      ),
      backgroundColor: Colors.blueGrey[200],
      body: Center(
        child: Image(
          image: NetworkImage(
              'https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.w3schools.com%2Fw3css%2Fw3css_images.asp&psig=AOvVaw2IxwyCZa7VaOiLr2lA9R2d&ust=1584460177773000&source=images&cd=vfe&ved=2ahUKEwjS8NyhrJ_oAhXt7uAKHURADeMQjRx6BAgAEAc'),
        ),
      ),
    ),
  ));
}

起初似乎有一个SocketException,我试图通过将代码行放在这里来解决this answer

现在,当我再次运行模拟器时,它的结尾是:

Performing hot restart...
Syncing files to device Android SDK built for x86...
Restarted application in 31,602ms.

════════ 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      NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:104:20)
<asynchronous suspension>
...
Image provider: NetworkImage("https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.w3schools.com%2Fw3css%2Fw3css_images.asp&psig=AOvVaw2IxwyCZa7VaOiLr2lA9R2d&ust=1584460177773000&source=images&cd=vfe&ved=2ahUKEwjS8NyhrJ_oAhXt7uAKHURADeMQjRx6BAgAEAc", scale: 1.0)
Image key: NetworkImage("https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.w3schools.com%2Fw3css%2Fw3css_images.asp&psig=AOvVaw2IxwyCZa7VaOiLr2lA9R2d&ust=1584460177773000&source=images&cd=vfe&ved=2ahUKEwjS8NyhrJ_oAhXt7uAKHURADeMQjRx6BAgAEAc", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════

我在这里错过了什么?

【问题讨论】:

    标签: android-studio http flutter exception dart


    【解决方案1】:

    您在网络图片中提供的 url 地址并不指向实际图片。试着像这样改变它

    image: NetworkImage('https://www.w3schools.com/w3css/img_lights.jpg')
    

    【讨论】:

    • 我正在阅读同一系列教程。我试图从谷歌图片中获取图片的地址。这就是为什么它发生在我身上,因为我选择的图像有一个在 shutterstock 上搜索的地址。这很令人困惑,但很有意义为什么该应用程序无法将整个网站变成图像。
    【解决方案2】:

    在复制图片网址后,请务必检查网址是否以“.jpg”、“.png”等结尾...如果它不以任何支持您将要获取的图片的有效扩展名结尾这个错误信息... 试试这样的:-

    import 'package:flutter/material.dart';
    
    void main() => runApp(
          MaterialApp(
            home: Scaffold(
              appBar: AppBar(
                title: Text("I am Rich"),
                backgroundColor: Colors.blueGrey[900],
              ),
              backgroundColor: Colors.blueGrey[200],
              body: Center(
                child: Image(
                  image: NetworkImage(
                      'https://cdn.pixabay.com/photo/2016/10/02/22/17/red-t-shirt-1710578_1280.jpg'),
                ),
              ),
            ),
          ),
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-21
      • 2016-07-12
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 2019-12-23
      • 2011-10-02
      • 1970-01-01
      相关资源
      最近更新 更多