Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I solve XMLHttpRequest error with flutter?

error while getting the url with http library:

Future<List<Sensor>> fetchSensores() async {
  final response = await http.get(
      Uri.parse(
          'https://firebasestorage.googleapis.com/v0/b/ecosolutions-73ab2.appspot.com/o/sensores.json?alt=media&token=09ac9e63-744d-4286-aae1-c4eadd81c026'),
      headers: {
        "Accept": "application/json",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "Access-Control-Allow-Origin, Accept"
      });
  if (response.statusCode == 200) {
    return compute(parseSensores, response.body);
  } else {
    throw Exception('Error');
  }
}

Error: XMLHttpRequest error.

like image 846
Diego jiménez Avatar asked Feb 28 '26 21:02

Diego jiménez


1 Answers

unfortunately, I have developed the app in Web Flutter and I found the Errors depend on XMLHttpRequest which happens when calling API. I tried just found one way that must run flutter run -d chrome --web-browser-flag "--disable-web-security" but this is not the solution and it doesn't work in the release. Adding CORS to headers also doesn't work.

So XMLHttpRequest error returns a null response body when API calls, the web Flutter side exists this problem.

This link must be useful yours, checked for fixed error https://www.youtube.com/watch?v=H-q-_0oGOzg

If you found the best way share me good luck

like image 131
Amin Avatar answered Mar 03 '26 11:03

Amin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!