title: Sending Post request on a button click generates XMLHttpRequest error in Flutter Web Application
body: <p>Flutter application below generates XMLHttpRequest error when the button is clicked.</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:http/http.dart' as http;
  
  void main() {
    runApp(MyApp());
  }
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return MaterialApp(home: HomePage());
   }
  }
  
  class HomePage extends StatefulWidget {
   HomePage() : super();
   @override
   _HomePageState createState() =&gt; _HomePageState();
  }
  
  class _HomePageState extends State&lt;HomePage&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
      title: const Text('Flutter WebView '),
    ),
    body: Center(
      child: TextButton(
        child: Text('Push me'),
        onPressed: save,
      ),
    ),
   );
  }
  
  void save() async {
   var sendString =
      '{&quot;name&quot;:&quot;MyCompany&quot;,&quot;address1&quot;:&quot;100 main &quot;,&quot;address2&quot;:&quot;Apt 202&quot;}';
   final response = await http.post(
    Uri.parse('http://localhost:8080/employers/nameAddress'),
    headers: &lt;String, String&gt;{
      'ContentType': 'application/json; charset=UTF-8',
    },
    body: sendString,
   );
   print(response.statusCode);
   print(response.contentLength);
   print(response.reasonPhrase);
   print(response.body);
  }
  }
  </code></pre>
  <p>The error generated is as follows:</p>
  <pre><code>Error: XMLHttpRequest error.
  dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 909:28    get current
  packages/http/src/browser_client.dart 71:22        &lt;fn&gt;
  dart-sdk/lib/async/zone.dart 1613:54               runUnary
  dart-sdk/lib/async/future_impl.dart 155:18         handleValue
  dart-sdk/lib/async/future_impl.dart 707:44         handleValueCallback
  dart-sdk/lib/async/future_impl.dart 736:13        _propagateToListeners
  dart-sdk/lib/async/future_impl.dart 533:           [_complete]
  dart-sdk/lib/async/stream_pipe.dart 61:11         _cancelAndValue
  dart-sdk/lib/async/stream.dart 1219:7                  &lt;fn&gt;
  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                &lt;fn&gt;
  at Object.createErrorWithStack (http://localhost:53992/dart_sdk.js:5054:12)
  at Object._rethrow (http://localhost:53992/dart_sdk.js:37670:16)
  at async._AsyncCallbackEntry.new.callback 
  (http://localhost:53992/dart_sdk.js:37666:13)
  at Object._microtaskLoop (http://localhost:53992/dart_sdk.js:37526:13)
  at _startMicrotaskLoop (http://localhost:53992/dart_sdk.js:37532:13)
  at http://localhost:53992/dart_sdk.js:33303:9
  </code></pre>
  <p>The following Dart application using the same save() function as above works fine without error.</p>
  <pre><code>import 'package:http/http.dart' as http;
  
  void main() async {
   save();
  }
  
  void save() async {
    var sendString =
    '{&quot;name&quot;:&quot;MyCompany&quot;,&quot;address1&quot;:&quot;100 main &quot;,&quot;address2&quot;:&quot;Apt 202&quot;}';
   final response = await http.post(
    Uri.parse('http://localhost:8080/employers/nameAddress'),
    headers: &lt;String, String&gt;{
    'ContentType': 'application/json; charset=UTF-8',
    },
    body: sendString,
   );
   print(response.statusCode);
   print(response.contentLength);
   print(response.reasonPhrase);
   print(response.body);
  </code></pre>
  <p>}</p>
  <p>I used the following Dart server to test both the Flutter app and Dart app. Server simply responds to any request it receives:</p>
  <pre><code>import 'dart:async';
  import 'package:shelf/shelf.dart' as shelf;
  import 'package:shelf/shelf_io.dart' as io;
  
  void main() async {
    print('server starting on 8080');
    httpServer = await io.serve(process, 'localhost', 8080);
  }
  
  var httpServer;
  FutureOr&lt;shelf.Response&gt; process(shelf.Request request) {
   print('got request');
   return shelf.Response.ok('Got request');
  }
  </code></pre>