Flutter: No 'Access-Control-Allow-Origin' header is present on the requested resource


I have created REST GET API using python Flask and it’s code is below

app = Flask(__name__)
def firstGetAPI():
        url = request.args.get('url')
        print(f'URL is {url}')
        res = "Success"
        response = flask.Response(res)
        response.headers.set('Access-Control-Allow-Origin', '*')
        response.headers.set('Access-Control-Allow-Headers', 'Content-Type,Authorization')
        response.headers.set('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
        return response, 200
    except Exception as e:
        response = {"status":"Fail", "message":str(e)}
        return json.dumps(response)
if __name__ == "__main__":
    app.run(host='', debug=True, port=8080)

Now on Frontend side the Flutter code is below

child: ElevatedButton(
                child: const Text('Download'),
                onPressed: () async {
                  var header = {
                    'Access-Control-Allow-Origin': '*',
                    "Accept": "application/json"

                  var url = Uri.parse(

                  print('URL is : ${url}');
                  var response = await http.get(url, headers: header);
                  print('Response status: ${response.statusCode}');
                  print('Response body: ${response.body}');

I have run this on Chrome Browser but I am always getting an error like below

     URL is :
:49831/#/:1 Access to XMLHttpRequest at ''
 from origin 'http://localhost:49831' has been blocked by CORS policy: Response to preflight 
 request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
browser_client.dart:74 GET net::ERR_FAILED

Sorry as I have no idea if this is problem with Python or Flutter, I have combined both code over here, Thanks


1st option

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

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’

2nd option

add header in python code header(‘Access-Control-Allow-Origin’, ‘*’)

Enable access control on simple HTTP server

Answered By – Gursewak Singh

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *