我一直在四处寻找如何使用 http 包及其 http.post() 方法访问和创建 Cloud Firestore 集合中的新文档 - 但似乎我遇到的所有文章都是 2019 年及之前的.无论我尝试什么,我都会不断收到以下错误:
Error: XMLHttpRequest error.
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 264:20 get current
packages/http/src/browser_client.dart 84:22 <fn>
dart-sdk/lib/async/zone.dart 1450:54 runUnary
dart-sdk/lib/async/future_impl.dart 143:18 handleValue
dart-sdk/lib/async/future_impl.dart 696:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 725:32 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 519:7 [_complete]
dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
dart-sdk/lib/async/stream.dart 1300: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 37275:58 <fn>
at Object.createErrorWithStack (http://localhost:49938/dart_sdk.js:4342:12)
at Object._rethrow (http://localhost:49938/dart_sdk.js:37882:16)
at async._AsyncCallbackEntry.new.callback (http://localhost:49938/dart_sdk.js:37876:13)
at Object._microtaskLoop (http://localhost:49938/dart_sdk.js:37708:13)
at _startMicrotaskLoop (http://localhost:49938/dart_sdk.js:37714:13)
at http://localhost:49938/dart_sdk.js:33226:9
Application finished.
我在 Flutter 中的代码:
Future<http.Response> _post() {
String url =
'https://firestore.googleapis.com/v1/projects/onebear-webapp/databases/(default)/collections/txt?key=AIzxSyAllNuEbN40DEvBPIQkcLIWn00c9TYWqY';
return http.post(
url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'title': 'title',
}),
);
}
我已阅读有关 CORS 的信息并尝试从 codewithandrea.com ( https://codewithandrea.com/videos/2020-05-25-top-8-tips-for-flutter-web-firebase/ ) 获取解决方案 - 仍然没有成功
感谢任何建议和指导,以使其发挥作用。
最佳答案
将其放在 header 参数中。
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",
"Access-Control-Allow-Methods": "POST, OPTIONS"
},
body: JSON.stringify(item);
关于firebase - 使用带有 Flutter Web App 的 http 包发布到 Cloud Firestore 及其 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64226729/
相关文章:
javascript - 来自 firebase 的图像的 html2canvas jsPdf 访问
reactjs - 在 useEffect Hook 中使用 axios 取消 token 时如何修
java - 如何将上下文传播到 Project Reactor 中的下游运算符?
javascript - 错误 : Jmeter: Typed variable declarati
asp.net-core - 我可以在 AspNetCore WebAPI 中将 Cancellat
python - 在嵌套/平面字符串和整数列表中查找超过 10 的最小数字
angular - 如何限制 NgSelect 下拉面板中显示的项目数?