Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS flutter web

I have a big problem that makes me late on my project I have the CROS problem that blocks my requests I can't correct it, I added in my .htacess the lines below, but it doesn't work I can't understand it

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://maps.googleapis.com/maps/api/place/autocomplete/json…nents=country:fr&key=MY_KEY. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)

RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.kosmos-digital.com/$1 [R,L]

Rewritecond %{HTTP_HOST} ^kosmos-digital.com$
Rewriterule ^(.*) https://www.kosmos-digital.com/$1 [QSA,L,R=301]

<ifModule mod_headers.c>
  SetEnvIf Origin "http(s)?://(www.kosmos-digital.com|www.maps.googleapis.com)$" AccessControlAllowOrigin=$0
  Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
  Header always set Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
</ifModule>

I don't think it's from my code, but I'll put it underneath :

try {
  await Dio().get<String>("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=28%20avenue%20pierre%20corneille&language=fr&components=country:fr&key=MY_KEY").then((e) {
  print(e.data);
}).catchError(print);
  // Map<String, dynamic> dataAsJson = await HttpUtils.getForJson(url, headers: headers);
  // print(dataAsJson.toString());
} catch(e) {
  print(e.toString());
}
like image 449
dafep Avatar asked Feb 28 '20 18:02

dafep


People also ask

How do you add CORS in flutter Web?

Adding CORS (Cross-Origin Resource Sharing) header You need to add the following CORS (Cross-Origin Resource Sharing) header as the header in your API code. This header means if the request is from Origin mentioned in the header, then handle the request. The CORS policy solution is recommended for development only.

What is CORS in Web API?

Cross-origin resource sharing (CORS) is a browser security feature that restricts cross-origin HTTP requests that are initiated from scripts running in the browser. If your REST API's resources receive non-simple cross-origin HTTP requests, you need to enable CORS support.


1 Answers

try that:

1- Go to flutter\bin\cache and remove a file named: flutter_tools.stamp

2- Go to flutter\packages\flutter_tools\lib\src\web and open the file chrome.dart.

3- Find '--disable-extensions'

4- Add '--disable-web-security'.

How to solve flutter web api cors error only with dart code?

if does not work, try those commands

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

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

like image 94
Mohammad Enawe Avatar answered Sep 22 '22 18:09

Mohammad Enawe