Background
I am using GitHub Codespaces, which does port forwarding for locally-hosted applications. So when I run flutter run -d web-server --web-hostname 127.0.0. --web-port
the localhost URL is mirrored with a githubpreview.dev
URL.
On the client side, I have a function to test the connection with the database:
static Future<bool> testConnection() async {
http.Response response = await _httpClient.get(
Uri.parse(baseUrl + 'test'), // requestObject.slug.string
headers: {
HttpHeaders.authorizationHeader: 'Bearer 69',
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers":
"Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS"
},
);
_httpClient
is a wrapper over HttpClient()
.
On the server side, I have the function as follows:
export function use_test(request) {
console.log(`test request:${request.headers.origin}`);
let options = {
"headers":{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers":
"Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
"Access-Control-Allow-Methods":
"GET, POST, PUT, PATCH, DELETE, OPTIONS"
},
"body": {
"payload": {
"msg": request['headers']
}
}
};
return ok(options);
}
Issue
Whenever testConnection
is run, the request is sent successfully, since the server logs the incoming request, but the response sent by the server is not received by the client, failing with an XMLHttpRequestError
. Now, I have come across this error many times when writing Flutter Web applications, but this one stumps me. Postman can receive the data all right, so I'm pretty sure CORS is to blame for this issue.
Additional Information
Flutter doctor output:
[✓] Flutter (Channel master, 3.1.0-0.0.pre.1354, on Debian GNU/Linux 11 (bullseye) 5.4.0-1074-azure, locale
en_US.UTF-8)
• Flutter version 3.1.0-0.0.pre.1354 on channel master at /workspaces/Lighthouse-Web/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision a30012b275 (3 days ago), 2022-06-22 17:04:07 -0700
• Engine revision fc08bf45b0
• Dart version 2.18.0 (build 2.18.0-216.0.dev)
• DevTools version 2.14.0
[✗] Android toolchain - develop for Android devices
✗ Unable to locate Android SDK.
Install Android Studio from: https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK components.
(or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, please use
`flutter config --android-sdk` to update to that location.
[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[✗] Linux toolchain - develop for Linux desktop
✗ clang++ is required for Linux development.
It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from
https://releases.llvm.org/
✗ CMake is required for Linux development.
It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from
https://cmake.org/download/
✗ ninja is required for Linux development.
It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from
https://github.com/ninja-build/ninja/releases
✗ pkg-config is required for Linux development.
It is likely available from your distribution (e.g.: apt install pkg-config), or can be downloaded from
https://www.freedesktop.org/wiki/Software/pkg-config/
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
[✓] Connected device (1 available)
• Linux (desktop) • linux • linux-x64 • Debian GNU/Linux 11 (bullseye) 5.4.0-1074-azure
[✓] HTTP Host Availability
• All required HTTP hosts are available
! Doctor found issues in 4 categories.
How can I fix this issue?