18

I was trying my hands on flutter web. I tried to connect a simple flutter web app I created to mysql database and localhost using the http package. However I dont get any data returned from the request method. When I tried to print out snaphot.error I got this: XMLHttpRequest error. I have this method in a FutureBuilder()

getMethod()async{
  String theUrl = 'https://localhost/fetchData.php';
  var res = await http.get(Uri.encodeFull(theUrl),headers: {"Accept":"application/json"});
  var responsBody = json.decode(res.body);
  print(responsBody);
  return responsBody;

}
4
  • Have you tried this request on you web browser or maybe postman? lets see the result
    – JideGuru
    May 15, 2019 at 8:21
  • 1
    Did you find any solution? I have asked the same question here: stackoverflow.com/questions/57340929/…
    – user7790873
    Aug 4, 2019 at 8:58
  • I also faced this issue, i changed CORS policy from Back end then it work like charm!! Jan 1, 2020 at 11:51
  • Hello @HardikBhalala what changes you made in Backend? Mar 22, 2021 at 5:44

3 Answers 3

7

You can also Add the code below to your php file like so:

<?php
require('connection.php');
header("Access-Control-Allow-Origin: *");
....
code goes here
....
?>

I Tried this on LocalHost and it worked.

NB: If you're using nodejs install the cors() package and use like

var express = require('express')
var app = express()
var cors = require('cors')

app.use(cors())

Check out the CORS package on npmjs

3
  • 1
    Thxs, this worked for me. All my flutter side API calls are going thru php scripts to "get" data. Is there any "gotchas" one should be aware of using this solution?
    – Soccerjf
    Mar 27, 2020 at 1:12
  • @Soccerjf Read More Here On Safety of CORS
    – Norbert
    May 27, 2020 at 10:25
  • where should i add this php snippet, i don't have a .php file in my flutter web project. should i add it to the index.html instead? May 29, 2021 at 21:36
3

I literally just stumbled over the error myself. You are falling afoul of CORS... if you trace the underlying network traffic, you should see that it sends an OPTIONS request first.

To get it to "work" temporarily, you can launch Chrome with CORS turned off. Obviously, this is not a long term solution, but it should get you going. The command you need is:

open /Applications/Google\ Chrome.app --args --disable-web-security --user-data-dir
5
  • Am on windows so ran this but this doesnt seem to work for me still start Chrome --args --disable-web-security --user-data-dir
    – Norbert
    May 15, 2019 at 10:09
  • Mmm... not sure. I wonder if the same options apply to windows. It definitely fixed the same symptoms for me on the Mac May 15, 2019 at 10:29
  • not work for me . Env: Mac 10.14.6 , Chrome 80.0.3987.10
    – Bono Lv
    Feb 15, 2020 at 8:34
  • 3
    open -n -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir="/tmp/chrome_dev_test" --disable-web-security worked for me on MAC!
    – Fintasys
    Mar 26, 2020 at 4:14
  • I'm searching for a good approach/long term solution. Can you explain or link any helpful references? Jul 16, 2020 at 12:22
0

What I found out and solved the issue is

For Accessing a website made by flutter basically you are calling a JS script

and for any website to give access to a script (which in my case was another website I was accessing the api from ) you need to give some sort of clearance to access

Which is in this case is CORS - Cross-Origin Resource Sharing

Add below code to your websites .htaccess file

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

adding this will now grant access to your flutter website to hit requests to your apis

9
  • I added .htaccess file above pubspec.yaml file with above content but still getting CORS error while running on localhost Jan 1, 2020 at 17:42
  • 8
    Where is the htaccess file?
    – Levi Lais
    Jan 29, 2020 at 17:05
  • it should be present on top of your file structure Jan 31, 2020 at 8:29
  • I can't find the .htaccess file.
    – devDeejay
    May 29, 2020 at 9:45
  • the file should be present on your server, and if it's not then you should create one and paste the content shown above May 29, 2020 at 9:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.