I am trying to use http package in my flutter web application but I am getting this error
Error: XMLHttpRequest error. dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get current packages/http/src/browser_client.dart 84:22 <fn> dart-sdk/lib/async/zone.dart 1612:54 runUnary dart-sdk/lib/async/future_impl.dart 152:18 handleValue dart-sdk/lib/async/future_impl.dart 704:44 handleValueCallback dart-sdk/lib/async/future_impl.dart 733:13 _propagateToListeners dart-sdk/lib/async/future_impl.dart 530:7 [_complete] dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue dart-sdk/lib/async/stream.dart 1219:7 <fn> dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14 _checkAndCall dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39 dcall dart-sdk/lib/html/dart2js/html_dart2js.dart 37307:58 <fn> at Object.createErrorWithStack (http://localhost:32961/dart_sdk.js:5348:12) at Object._rethrow (http://localhost:32961/dart_sdk.js:39350:16) at async._AsyncCallbackEntry.new.callback (http://localhost:32961/dart_sdk.js:39344:13) at Object._microtaskLoop (http://localhost:32961/dart_sdk.js:39176:13) at _startMicrotaskLoop (http://localhost:32961/dart_sdk.js:39182:13) at http://localhost:32961/dart_sdk.js:34689:9
I have also tried adding headers
headers: {
"Accept": "application/json",
"Access-Control_Allow_Origin": "*"
});
but still getting the same error
here is flutter run --verbose
[ +199 ms] executing: [/home/viral/snap/flutter/common/flutter/] git -c log.showSignature=false log -n 1
--pretty=format:%H
[ +99 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[ ] c5a4b4029c0798f37c4a39b479d7cb75daa7b05c
[ +1 ms] executing: [/home/viral/snap/flutter/common/flutter/] git tag --points-at
c5a4b4029c0798f37c4a39b479d7cb75daa7b05c
[ +33 ms] Exit code 0 from: git tag --points-at c5a4b4029c0798f37c4a39b479d7cb75daa7b05c
[ ] 2.0.1
[ +98 ms] executing: [/home/viral/snap/flutter/common/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[ +10 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[ ] origin/stable
[ ] executing: [/home/viral/snap/flutter/common/flutter/] git ls-remote --get-url origin
[ +10 ms] Exit code 0 from: git ls-remote --get-url origin
[ ] https://github.com/flutter/flutter.git
[ +98 ms] executing: [/home/viral/snap/flutter/common/flutter/] git rev-parse --abbrev-ref HEAD
[ +10 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[ ] stable
[ +144 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[ +4 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +189 ms] executing: /home/viral/Android/Sdk/platform-tools/adb devices -l
[ +83 ms] List of devices attached
[ +8 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[ +3 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[ ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[ +183 ms] Skipping pub get: version match.
[ +74 ms] Found plugin flutter_downloader at
/home/viral/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.5.2/
[ +553 ms] Found plugin flutter_downloader at
/home/viral/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.5.2/
[ +78 ms] Generating
/home/viral/work/github/instafy/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
[ +707 ms] Launching lib/main.dart on Chrome in debug mode...
[ +321 ms] Updating assets
[ +220 ms] Waiting for connection from debug service on Chrome...
[ +28 ms] Found plugin flutter_downloader at
/home/viral/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.5.2/
[ +66 ms] Found plugin flutter_downloader at
/home/viral/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.5.2/
[ +150 ms] <- reset
[ +14 ms] /home/viral/snap/flutter/common/flutter/bin/cache/dart-sdk/bin/dart --disable-dart-dev
/home/viral/snap/flutter/common/flutter/bin/cache/artifacts/engine/linux-x64/frontend_server.dart.snapshot --sdk-root
/home/viral/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/ --incremental --target=dartdevc
--debugger-module-names --experimental-emit-debug-metadata -DFLUTTER_WEB_AUTO_DETECT=true --output-dill
/tmp/flutter_tools.MEXTZS/flutter_tool.CGSCEK/app.dill --libraries-spec
file:///home/viral/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/libraries.json --packages
/home/viral/work/github/instafy/.dart_tool/package_config.json -Ddart.vm.profile=false -Ddart.vm.product=false
--enable-asserts --track-widget-creation --filesystem-root /tmp/flutter_tools.MEXTZS/flutter_tools.IBTMJF
--filesystem-scheme org-dartlang-app --initialize-from-dill
build/c7922d95bf4a2462b75c84a97c312edb.cache.dill.track.dill --platform
file:///home/viral/snap/flutter/common/flutter/bin/cache/flutter_web_sdk/kernel/flutter_ddc_sdk.dill
--no-sound-null-safety
[ +24 ms] <- compile org-dartlang-app:/web_entrypoint.dart
[+49014 ms] Waiting for connection from debug service on Chrome... (completed in 49.3s)
[ +1 ms] Synced 29.8MB.
[ ] <- accept
[ ] Caching compiled dill
[ +208 ms] Using Google Chrome 89.0.4389.90
[ +760 ms] [CHROME]:
[ +1 ms] [CHROME]:DevTools listening on ws://127.0.0.1:33683/devtools/browser/c7a1708a-1d3e-4132-b815-50c558a9aea7
[ +610 ms] DwdsInjector: Received request for entrypoint at http://localhost:41555/main_module.bootstrap.js
[ +14 ms] MetadataProvider: Loading debug metadata...
[ +30 ms] MetadataProvider: Loaded debug metadata
[ +16 ms] DwdsInjector: Injected debugging metadata for entrypoint at
http://localhost:41555/main_module.bootstrap.js
[+5286 ms] DevHandler: Debug service listening on ws://127.0.0.1:45929/0Ik9Iep1NY4=/ws
[ +22 ms] Debug service listening on ws://127.0.0.1:45929/0Ik9Iep1NY4=/ws
[ ] Running with unsound null safety
[ ] For more information see https://dart.dev/null-safety/unsound-null-safety
[ +5 ms] 🔥 To hot restart changes while running, press "r" or "R".
[ +3 ms] For a more detailed help message, press "h". To quit, press "q".
[+12541 ms] Error: XMLHttpRequest error.
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get
current
packages/http/src/browser_client.dart 84:22 <fn>
dart-sdk/lib/async/zone.dart 1612:54 runUnary
dart-sdk/lib/async/future_impl.dart 152:18 handleValue
dart-sdk/lib/async/future_impl.dart 704:44
handleValueCallback
dart-sdk/lib/async/future_impl.dart 733:13
_propagateToListeners
dart-sdk/lib/async/future_impl.dart 530:7 [_complete]
dart-sdk/lib/async/stream_pipe.dart 61:11
_cancelAndValue
dart-sdk/lib/async/stream.dart 1219:7 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39 dcall
dart-sdk/lib/html/dart2js/html_dart2js.dart 37307:58 <fn>
at Object.createErrorWithStack (http://localhost:41555/dart_sdk.js:5348:12)
at Object._rethrow (http://localhost:41555/dart_sdk.js:39350:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:41555/dart_sdk.js:39344:13)
at Object._microtaskLoop (http://localhost:41555/dart_sdk.js:39176:13)
at _startMicrotaskLoop (http://localhost:41555/dart_sdk.js:39182:13)
at http://localhost:41555/dart_sdk.js:34689:9
I am using XAMPP control panel and using a local server like Apache port no 80, but in flutter web getting XMLHttpRequest error and in mobile device. The same coding fetches data using API not getting any error.
How to fetch data using API while app run on flutter-web?
Error
Launching libmain.dart on Chrome in debug mode...
Syncing files to device Chrome...
Debug service listening on ws://127.0.0.1:56619/FsXy3a4ZrZg=
Debug service listening on ws://127.0.0.1:56619/FsXy3a4ZrZg=
Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get current
packages/http/src/browser_client.dart 84:22 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1450:54 runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 143:18 handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 696:44 handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 725:32 _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 519:7 [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1302:7 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14 _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39 dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37312:58 <fn>
at Object.createErrorWithStack (http://localhost:5555/dart_sdk.js:4361:12)
at Object._rethrow (http://localhost:5555/dart_sdk.js:38189:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:5555/dart_sdk.js:38183:13)
at Object._microtaskLoop (http://localhost:5555/dart_sdk.js:38015:13)
at _startMicrotaskLoop (http://localhost:5555/dart_sdk.js:38021:13)
at http://localhost:5555/dart_sdk.js:33518:9
Backend Side PHP Webservice
<?php
header("Access-Control_Allow_Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Content-type:application/json;charset=utf-8");
header("Access-Control-Allow-Methods: GET");
include 'config.php';
$sql="select * from calinsert";
$result=mysqli_query($conn,$sql)or die("query failed");
if(mysqli_num_rows($result) >0){
$output=mysqli_fetch_all($result,MYSQLI_ASSOC);
echo json_encode($output);
}
else{
echo json_encode(array('message'=>'no record found','status'=>false));
}
?>
Flutter Side Code
Future getdata()async {
final response = await http.get(
'http://localhost:80/web_service/calview.php',
headers: {
"Accept": "application/json",
"Access-Control_Allow_Origin": "*"
});
print(response.statusCode);
print(response.body);
}
I have Solved my problem, and not going to delete this question because there aren’t many well-defined solutions to this problem.
For Future viewer who is using flutter web and AWS API-gateway.
- if you encounter this problem it means its from backend side not from flutter side
- XMLHttpRequest error. is caused due to CORS
The solution to the problem you have to enable CORS in api-gateway follow this link.
but if you are using proxy integration with lambda and api-gateway then in that case enabling CORS doesn’t going to help, you have to pass on headers from the response of lambda function. like
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
"Access-Control-Allow-Credentials": true, // Required for cookies, authorization headers with HTTPS
"Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
"Access-Control-Allow-Methods": "POST, OPTIONS"
},
body: JSON.stringify(item)
};
the format needs to be the same. also, one particular question that helps me a lot to understand this whole issue is going through the various answer of the question link.
Now comes my problem, what I’m doing wrong i that i am passing "Access-Control-Allow-Origin": "*", from frontend and enabling CORS in API gateway also send similar headers which are creating a problem for me
Access to XMLHttpRequest at 'API-URL' from origin 'http://localhost:63773' 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. //this particular line
So after changing my function to this everything works perfectly fine
fetchData() async {
String url =
"API-url";
Map<String, String> headers = {
"Content-Type": "text/plain",
};
String json = '{"emailId":"emailId"}';
Map<String, String> map = Map();
map["emailId"] = "fake@gmail.com";
http.Response response = await http
.post(Uri.parse(url), headers: headers, body: jsonEncode(map))
.then((value) {
print("onThen> " + value.body.toString());
}).onError((error, stackTrace) {
print("onError> " +
error.toString() +
" stackTrace> " +
stackTrace.toString());
});
}
This is the error from http package in Flutter:
Error: XMLHttpRequest error.
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 908:28 get current
packages/http/src/browser_client.dart 69:22 <fn>
dart-sdk/lib/async/zone.dart 1687:54 runUnary
dart-sdk/lib/async/future_impl.dart 160:18 handleValue
dart-sdk/lib/async/future_impl.dart 767:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 796:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 593:7 [_complete]
dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
dart-sdk/lib/async/stream.dart 1232:7 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39 dcall
dart-sdk/lib/html/dart2js/html_dart2js.dart 37332:58 <fn>
at Object.createErrorWithStack (http://localhost:46627/dart_sdk.js:5074:12)
at Object._rethrow (http://localhost:46627/dart_sdk.js:38925:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:46627/dart_sdk.js:38921:13)
at Object._microtaskLoop (http://localhost:46627/dart_sdk.js:38778:13)
at _startMicrotaskLoop (http://localhost:46627/dart_sdk.js:38784:13)
at http://localhost:46627/dart_sdk.js:34519:9
This error is a Javascript error and it can be inspected from AndroidStudio terminal or from the browser console.
If you run into this problem it means that the requests to the API server are failing due to a CORS error.
When executing these types of requests from the web page, a sort of “pre authorization request” is made to the server. This request is an OPTIONS request that must be answered with a 2xx response. This is called PreFlight authorization. Make sure that your application is accepting the OPTIONS method.
A second step is the request itself (for example with GET method).
To allow a correct answer from the server, you need to add some headers to the response. In case you are using Apache and your API are public, you have to set the following headers (Apache configuration in this case):
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Content-Type"
Header set Referrer-Policy "no-referrer-when-downgrade"
</IfModule>
Take a look to Mozilla knowledge for more information about CORS and security concerns. To use authentication such as cookies, you cannot use the wildcard “*” in Access-Control-Allow-Origin header. This means that you have to write explicitly the origin url of the requests.
Moreover, CORS behavior is different between browser and apps. Dive into the topic in this interesting Reddit post.
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.
[Solved] Getting XMLHttpRequest error in Flutter Web for Razorpay API call using http post
Question
Asked by Himanshu on January 05, 2022 (source).
I am getting a XMLHttpRequest error in Flutter Web for Razorpay API call using http post from past 4 days I searched on whole internet but no solution worked in my case.
Below is my Post request code using http: ^0.13.4 package:
Future<String> postRequest(String amount) async {
var url = Uri.parse('https://api.razorpay.com/v1/orders');
var key = 'rzp_test_Xx7Zw7hLfzozf6';
var secret = '7PgbvLaODVzRbb9oRcaqCZMi';
var auth = 'Basic ' + base64Encode(utf8.encode('$key:$secret'));
print(auth);
var body = json
.encode({"amount": 50000, "currency": "INR", "receipt": "rcptid_11"});
var id = DateTime.now().millisecondsSinceEpoch.remainder(100000).toString();
var client = http.Client();
var response = await client.post(
url,
headers: {
'Content-Type': 'application/json',
'Authorization': auth,
},
body: body,
);
print(json.decode(response.body));
return json.decode(response.body);
}
The Curl command:
curl -X POST https://api.razorpay.com/v1/orders-u <YOUR_KEY_ID>:<YOUR_SECRET>-H 'content-type:application/json'-d '{ "amount": 50000, "currency": "INR", "receipt": "rcptid_11"}'
The complete error:
Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get current
packages/http/src/browser_client.dart 69:22 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1687:54 runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 160:18 handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 767:44 handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 796:13 _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 593:7 [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1288:7 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14 _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39 dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37254:58 <fn>
at Object.createErrorWithStack (http://localhost:62444/dart_sdk.js:5076:12)
at Object._rethrow (http://localhost:62444/dart_sdk.js:40477:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:62444/dart_sdk.js:40473:13)
at Object._microtaskLoop (http://localhost:62444/dart_sdk.js:40330:13)
at _startMicrotaskLoop (http://localhost:62444/dart_sdk.js:40336:13)
at http://localhost:62444/dart_sdk.js:35811:9
I have tried every solution over internet but nothing worked.
Answer
Question answered by Richard H (source).
This smells of a CORS error, so I just pasted your code into Dartpad, opened the Chrome console and hit run. As expected, it was a CORS error.
Next step was then to search api razorpay cors, which threw up a bunch of results all complaining about the same error (all with no solution — which doesn’t bode well — typically means there isn’t a solution).
Next step was to notice that no decent payment API would have you include credentials into a client side app, especially a browser app. A simple inspection of the headers in the Chrome console reveals your username/password credentials. The implication of this is that you’re using the API incorrectly.
This is quickly confirmed by looking at the Razorpay website which confirms that the order api is a Server-Server api. Another clue is that all the sample code for this api is in languages like PHP, Java, Node.js, etc — i.e. for the back end.
Answer: you must not call this API from the Flutter app. It must be called from your back end.
- Source: Stackoverflow.com
896 votes
5 answers


Get the solution ↓↓↓
I am using XAMPP control panel and using a local server like Apache port no 80, but in flutter web getting XMLHttpRequest error and in mobile device. The same coding fetches data using API not getting any error.
How to fetch data using API while app run on flutter-web?
Error
Launching libmain.dart on Chrome in debug mode...
Syncing files to device Chrome...
Debug service listening on ws://127.0.0.1:56619/FsXy3a4ZrZg=
Debug service listening on ws://127.0.0.1:56619/FsXy3a4ZrZg=
Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get current
packages/http/src/browser_client.dart 84:22 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1450:54 runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 143:18 handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 696:44 handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 725:32 _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 519:7 [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1302:7 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 324:14 _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 329:39 dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37312:58 <fn>
at Object.createErrorWithStack (http://localhost:5555/dart_sdk.js:4361:12)
at Object._rethrow (http://localhost:5555/dart_sdk.js:38189:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:5555/dart_sdk.js:38183:13)
at Object._microtaskLoop (http://localhost:5555/dart_sdk.js:38015:13)
at _startMicrotaskLoop (http://localhost:5555/dart_sdk.js:38021:13)
at http://localhost:5555/dart_sdk.js:33518:9
Backend Side PHP Webservice
<?php
header("Access-Control_Allow_Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Content-type:application/json;charset=utf-8");
header("Access-Control-Allow-Methods: GET");
include 'config.php';
$sql="select * from calinsert";
$result=mysqli_query($conn,$sql)or die("query failed");
if(mysqli_num_rows($result) >0){
$output=mysqli_fetch_all($result,MYSQLI_ASSOC);
echo json_encode($output);
}
else{
echo json_encode(array('message'=>'no record found','status'=>false));
}
?>
Flutter Side Code
Future getdata()async {
final response = await http.get(
'http://localhost:80/web_service/calview.php',
headers: {
"Accept": "application/json",
"Access-Control_Allow_Origin": "*"
});
print(response.statusCode);
print(response.body);
}
2021-12-2
Write your answer
798
votes


Answer
Solution:
In most cases of Flutter API use, addAccess-Control-Allow-Origin value in header might resolve the issue. (Note: This will help in access the local or external APIs)
header("Access-Control-Allow-Origin: *");
Hint: you have typo in your above header, please check and correct.
312
votes


Answer
Solution:
-
Fix the typo (replace underscores by dashes):
header("Access-Control-Allow-Origin: *"); -
Add the following header in your php code:
header("Access-Control-Allow-Headers": "Access-Control-Allow-Origin, Accept");
575
votes


Answer
Solution:
Just removed the header attribute from the post method and it works for me.
207
votes


Answer
Solution:
I was recently getting this error as well and I seem to have fixed by upgrading my package to the most recent0.12.2 version and it seems to have helped — so far.
https://pub.dev/packages/http
1
votes


Answer
Solution:
Thank you!
F12 Tools -> Access to XMLHttpRequest at ‘https://localhost:44360/xxyy’ from origin ‘http://localhost:52273’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
More info — CORS is usually a sever side issue, you have to set up what clients are allowed. For development purposes, my flutter web is talking to a .net core service so I had to enable CORS in development mode only to allow CORS using the following:
builder
.AllowAnyOrigin()
.WithHeaders(HeaderNames.AccessControlAllowHeaders, "Content-Type")
.AllowAnyMethod();
}));
Share solution ↓
Additional Information:
Date the issue was resolved:
2021-12-2
Link To Source
Link To Answer
People are also looking for solutions of the problem: sqlstate[hy000] [1698] access denied for user ‘root’@’localhost’
Didn’t find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.



