When sending http requests to a google api (directions api) with http package I get this error:
Error: XMLHttpRequest error.
dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 963:28 get current
packages/http/src/browser_client.dart 69:22 <fn>
dart-sdk/lib/async/zone.dart 1685:54 runUnary
dart-sdk/lib/async/future_impl.dart 147:18 handleValue
dart-sdk/lib/async/future_impl.dart 766:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 795:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 557:7 [_complete]
dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
dart-sdk/lib/async/stream.dart 1530: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 37309:58 <fn>
at Object.createErrorWithStack (http://localhost:54316/dart_sdk.js:5093:12)
at Error._throw (http://localhost:54316/dart_sdk.js:20399:18)
at Error.throwWithStackTrace (http://localhost:54316/dart_sdk.js:20396:18)
at async._AsyncCallbackEntry.new.callback (http://localhost:54316/dart_sdk.js:40921:18)
at Object._microtaskLoop (http://localhost:54316/dart_sdk.js:40778:13)
at _startMicrotaskLoop (http://localhost:54316/dart_sdk.js:40784:13)
at http://localhost:54316/dart_sdk.js:36261:9
Here is my http request:
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'constants.dart';
class Directions {
Future<void> getRouteInfo() async {
final url = Uri.parse(
'https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=$googleAPIKey');
final response = await http.post(url);
print(response.statusCode);
final relevantBody = json.decode(response.body)['routes'][0]['legs'][0];
}
}
My google account has billing active. The API key is working with other google apis in the same Flutter app and getting no errors. And when I copy & paste the url from the request to the browser I get the correct result. When I try to print the response status I can't because the error is thrown before and there is no response.
I know there is one solution that involves disabling web security, but this only works locally, I need this to work in production. Is this CORS related?