Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter cant load image from url

Tags:

flutter

════════ Exception caught by image resource service ════════════════════════════ The following ImageCodecException was thrown resolving an image codec: Failed to load network image. Image URL: https://cdn.sportmonks.com/images/soccer/leagues/5.png Trying to load an image from another domain? Find answers at: https://flutter.dev/docs/development/platform-integration/web-images

When i try the demo URL https://picsum.photos/250?image=9 it is working but the url above is good so what can be the problem?

class ListRow extends StatelessWidget {
  final String name;
  final imageUrl;
  const ListRow({Key key, this.name, this.imageUrl}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Container(
          width: 18,
          height: 18,
          child: Image(
            image: NetworkImage(
                'https://cdn.sportmonks.com/images/soccer/leagues/5.png'),
          ),
        ),
        SizedBox(width: 10),
        Flexible(
          child: new Text(
            name,
            style:
                new TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
          ),
        ),
      ],
    );
  }
}
like image 517
nadav Avatar asked Jan 24 '21 15:01

nadav


People also ask

How do I load an image from URL Flutter?

Displaying images is fundamental for most mobile apps. Flutter provides the Image widget to display different types of images. To work with images from a URL, use the Image.network() constructor.

Why is my network image not showing up on Flutter?

“flutter network image not showing” Code Answer's Try to add internet permission in android folder(do nothing with IOS part). Or jsut reinstall app.

How do I display an image on Flutter Web?

Use <img> in a platform view. Flutter supports embedding HTML inside the app using HtmlElementView . Use it to create an <img> element to render the image from another domain. However, do keep in mind that this comes with the limitations explained in the section “Flutter renderers on the web” above.


3 Answers

I get the same error, for a couple of weeks now, when trying to run an app from the master channel.

I got it working by forcing the web renderer to be HTML:

flutter run -d chrome --no-sound-null-safety --web-renderer=html

When you build your app for web you should:

flutter build web --no-sound-null-safety --web-renderer=html

By default the web renderer is auto, choosing the canvaskit web renderer on desktop browsers and html on mobile.

If you want to avoid this altogether you can stay on the beta or dev channels.

like image 114
Constantin Avatar answered Nov 11 '22 07:11

Constantin


So flutter web has upgraded default rendered(HTML) -> CanvasKit and it has better performance.

You are most likely getting this error because CORS(can check chrome network tab to be sure).

To solve it could:

  1. Update cors settings server side: https://stackoverflow.com/a/66104543/4679965

  2. Run app with old rendered:

flutter run -d chrome --web-renderer html // to run the app

flutter build web --web-renderer html --release // to generate a production build
  1. Or display images with platform view:
import 'dart:html';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;

class MyImage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    String imageUrl = "image_url";
    // https://github.com/flutter/flutter/issues/41563
    // ignore: undefined_prefixed_name
    ui.platformViewRegistry.registerViewFactory(
      imageUrl,
      (int _) => ImageElement()..src = imageUrl,
    );
    return HtmlElementView(
      viewType: imageUrl,
    );
  }
}

like image 32
Nuts Avatar answered Nov 11 '22 06:11

Nuts


if it is working with another image then it should be a server-side error where the image is stored.

like image 44
Khawaja Furqan Avatar answered Nov 11 '22 05:11

Khawaja Furqan