Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Web/Dart CORS Error with Firebase Hosting

Problem

I wrote an application in Flutter Web. When I run it in the Browser (debug), I get this error:

cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.googleapis.com/identityto...

When I run it in release mode, I just get this:

Error while fetching an original source: NetworkError when attempting to fetch resource.
Source URL: org-dartlang-sdk:///sdk/lib/_internal/js_runtime/lib/js_helper.dart

Other info

  • The App is hosted in Firebase Hosting but the error also occurs on localhost without Firebase
  • I think the Problem is cors in both cases but the release mode just has less logs

What I tried

According to this Documentation or this Question I have to add something using Expressjs like:

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({ origin: true }));
  • Is there anything like Expressjs in Dart/Flutter? I saw this but I could not get it to work.
  • Or is there another way to set the headers?
like image 483
Stein. Avatar asked Feb 19 '20 21:02

Stein.


People also ask

Is Cors error in flutter web field common?

It seems like CORS error is well-known issue in the web field. But I tried flutter web for the first time ever and I faced critical error. The code below worked well in app version when it was running on iOS device, but when i tested the same code on Chrome with web debugging from beta channel, it encountered CORS error.

How do I use firebase with flutterfire?

Before using FlutterFire on the web, you must first import the Firebase JavaScript SDK and initialize Firebase. The only way to currently add the Firebase SDKs to your Flutter web project is by importing the scripts from the Firebase content delivery network (CDN). Add the firebase-app.js script to your index.html file: ...

What is Cors proxy flutter?

This CORS proxy solution is only for students or developers learning networking topics in their programming language. When the Flutter web app sends a request to this 3rd party, it adds the necessary CORS header and returns the original server response to the Flutter web app.

Is Cors blocking me from using Firebase?

It seems that indeed, CORS is blocking you from using Firebase to store and access data. As per the documentation Configuring cross-origin resource sharing (CORS):


1 Answers

It seems that indeed, CORS is blocking you from using Firebase to store and access data. As per the documentation Configuring cross-origin resource sharing (CORS):

Cross Origin Resource Sharing (CORS) is a mechanism for allowing interactions between resources from different origins, something that is normally prohibited in order to prevent malicious behavior.

Considering that, you will need to configure yours to work and accept requests made via HTTP. I would recommend you to take a look at the above documentation and the following links, so you can get more information about it.

  • Enabling CORS in Cloud Functions for Firebase
  • Call functions via HTTP requests

Once you have them configured, you should not face both errors anymore, since the requests via HTTP will be authorized via CORS and the access to Firebase should be established.

Let me know if the information helped you!

like image 99
gso_gabriel Avatar answered Sep 19 '22 14:09

gso_gabriel