In web Flutter app hosted on Firebase Hosting, I try to make some Google Api calls.
I already try to configure cors for Firebase Hosting, it works for images downloaded from Firebase Storage, but do not works for Google Api calls…
Like : access-control-allow-origin is not allowed error , but if omitted then is expected from get request Flutter web
I spent hours to try configuration.
Here is Flutter code :
final Map<String, dynamic> parameters = <String, dynamic>{
'place_id': placeId,
'fields': 'address_component,geometry',
'key': mapsApiKey,
'sessiontoken': sessionToken
};
final Uri request = Uri(
scheme: 'https',
host: 'maps.googleapis.com',
path: '/maps/api/place/details/json',
queryParameters: parameters);
final response = await client.get(request);
2
Answers
CORS errors would not be coming from Firebase Hosting, but from the Google APIs themselves. CORS is a browser side safety mechanism, where the browser sends a "preflight" (an OPTIONS call) to the
fetch()
orUri()
URL that your code is about to call to find out from which hosts a page calling that API should be. If the API (in this case,maps.googleapis.com
) does not handle the OPTIONS request or replies with a restricted list, then your browser throws a CORS exception if the page making that API call is not coming from one of those restricted hosts.The most simple way to solve CORS error is to add the following code in you
project_root/web/index.html