Flutter web can't load network image from another domain

34,814

Solution 1

For being able to display your images from any other Domain or from Firebase Storage on a Flutter web page you have to configure your data for CORS.

  1. Open the GCP console, select your project and start a cloud terminal session by clicking the >_ icon button in the top navbar.

  2. Click the open editor button (pencil icon), then create the cors.json file.

The cors.json file should look like this:

[
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]

I set the origin to * which means that every website can display your images. But you can also insert the domain of your website there to restrict access.

  1. Run gsutil cors set cors.json gs://your-bucket

If you need more information: https://cloud.google.com/storage/docs/configuring-cors

Solution 2

There are two ways to resolve this either run your app using HTML render or setup the CORS configuration.

1. Using HTML render

Taken from the docs

CORS is a mechanism that browsers use to control how one site accesses the resources of another site. It is designed such that, by default, one web-site is not allowed to make HTTP requests to another site using XHR or fetch. This prevents scripts on another site from acting on behalf of the user and from gaining access to another site’s resources without permission

When using <img>, <picture>, or <canvas>, the browser automatically blocks access to pixels when it knows that an image is coming from another site and the CORS policy disallows access to data.

Flutter has two renderers for web, canvaskit and html When you run/build app on the flutter web it uses renderers based on the device where its running.

HTML renderer: when the app is running in a mobile browser.

CanvasKit renderer: when the app is running in a desktop browser.

auto (default) - automatically chooses which renderer to use.

The HTML renderer can load cross-origin images without extra configuration. so you could use these commands to run and build the app.

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

flutter build web --web-renderer html --release // to generate a production build

source: https://docs.flutter.dev/development/tools/web-renderers

2. Setup CORS Configuration

  • Download the Google-cloud-sdk which contains a tool called gsutil
  • In your flutter project create a file called cors.json and add this json file which will remove all domain restrictions.
[
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]
  • Run gcloud init // located in google-cloud-sdk/bin
  • Authenticate yourself by clicking the link and choose the project in the console
  • finally execute gsutil cors set cors.json gs://<your-bucket-name>.appspot.com You can find your bucket name in firebase storage.

Solution 3

i solve this issue by using html renderer

flutter build web --release --web-renderer html

or

flutter run --web-renderer html

Solution 4

for me flutter run -d chrome --web-renderer html worked.

Solution 5

If you use firebase storage just follow these steps:

  1. Open Google Cloud Console at your project
  2. Click on console icon in top right corner
  3. Click Open editor
  4. Click File->New->cors.json
  5. Place code below
[
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]
  1. Then Run in console

**

gsutil cors set cors.json gs://bucket-name

**

bucket-name is the name of the storage bucket which you can find on your firebase project above the folders in the storage section

Share:
34,814
Abel Ayalew
Author by

Abel Ayalew

Electrical Engineering Student @AASTU and computer enthusiast!

Updated on February 13, 2022

Comments

  • Abel Ayalew
    Abel Ayalew about 2 years

    I can't load network images in flutter web from other domains with API calls. getting this error

    Trying to load an image from another domain? Find answers at: https://flutter.dev/docs/development/platform-integration/web-images ImageCodecException: Failed to load network image.

    any help?

  • Abel Ayalew
    Abel Ayalew about 3 years
    it works for both deployment and debuging
  • theSpuka
    theSpuka about 3 years
    On Step 2, in order to create the cors.json file, click on the 3 dots (...) on the same row as Explorer:XXXX OR just click on File>new file. Step 3: You'll find "your-bucket" in your firebase STORAGE console and YOU WILL HAVE TO AUTHORIZE when you run the shell command.
  • Muthu S
    Muthu S almost 3 years
    not loading for google places API for flutter web
  • Lenin Zapata
    Lenin Zapata almost 3 years
    This command is executed every time a change is copied in Flutter ?, this command works but when re-executing the changes no longer work
  • Liel van der Hoeven
    Liel van der Hoeven almost 3 years
    To verify: gsutil cors get gs://your-bucket
  • Abdulmalek Dery
    Abdulmalek Dery almost 3 years
    it will make the image and text density so bad...please don't use that type of rendering
  • James 666
    James 666 almost 3 years
    Thanks Jens and Spuka, that really saved me - don't know where the hell you got that solution from
  • Raghu Mudem
    Raghu Mudem over 2 years
    Still not working after the all above suggestions, Trying to display YouTube thumbnails in my flutter web app. Am I missing something after point 3. Is there any reload or refresh command need to perform. Please suggest me.
  • Raghu Mudem
    Raghu Mudem over 2 years
    @MuthuS did you find the solution?
  • Muthu S
    Muthu S over 2 years
    No, unfortunately no solutions were worked in my case. Luckily its my internal project, so i used to enable CORS plug-in for users browsers to make it work.
  • kishan vekariya
    kishan vekariya over 2 years
    i want to add fit property like image widget, any idea how to add that ?
  • Omatt
    Omatt over 2 years
    The arguments work when deployed. You just need to add the same arguments during build flutter build web --web-renderer html
  • sh_ark
    sh_ark over 2 years
    This looks like configuration only related to firebase and google cloud. Is there any additional configuration involved in flutter app as well? As images are still not loading and throws missing CORS headers error.
  • sh_ark
    sh_ark over 2 years
    Okay, so it's working for me, I was running in localhost and also using origin as "https://firebasestorage.googleapis.com/v0/b/my_app_name.app‌​spot.com", which on changing to "*" starts working. But how to correctly use cutom origin instead of "*"?
  • nipunasudha
    nipunasudha over 2 years
    @sh_ark use https://my_app_name.we.app this is the domain the request is coming from
  • Priyshrm
    Priyshrm over 2 years
    This is a great answer. Worked like a charm in my flutter project. Why does it not have more upvotes? Am I missing anything?
  • Franz
    Franz over 2 years
    That worked for me. Thank you :)
  • santosh adhikari
    santosh adhikari over 2 years
    Did not work in my case
  • Ali Azimoshan
    Ali Azimoshan over 2 years
    for people like me who don't know anything about buckets and get 403 errors, you get your bucket List with gsutil ls, and check which one is used for your app and set cors.json for that one.
  • oga
    oga over 2 years
    Very good to have this at a glance. Thank you.
  • Yves Boutellier
    Yves Boutellier about 2 years
    where in your file tree do you place the cors.json file? (does it matter?)
  • Mahesh Jamdade
    Mahesh Jamdade about 2 years
    cors.json doesn't really matter once you run the gsutil command.
  • Hassan Hammad
    Hassan Hammad about 2 years
    thank you so much. Wouldnt have figured this out in a million years lol
  • Leonardo Rignanese
    Leonardo Rignanese about 2 years
    To verify: 'gsutil cors get gs://your-bucket'
  • Hamza
    Hamza about 2 years
    As simple as that, what a solution 2!!
  • Inder Pal Singh
    Inder Pal Singh almost 2 years
    @MaheshJamdade cors.json file is actually required for gsutil to read our cors configuration.Also Cors.json file should be placed in path where you'll run G-Cloud command
  • most200
    most200 almost 2 years
    Adding cors.json in the web folder does not work by itself. You have to follow the instructions in other answers to apply the file to your storage bucket using gsutil. And it does not have to be in the web folder, it can be saved anywhere.
  • Noobdeveloper
    Noobdeveloper almost 2 years
    what is maxageseconds in this code
  • Sadam Khan
    Sadam Khan almost 2 years
    thank you. One of the simplest solutions and it worked for me. Just had to save, hot reload, and reload the web page. Thanks
  • Ali Murtaza
    Ali Murtaza almost 2 years
    It worked! In my case, I re-run the web app and it showed the image