错误:使用天气 API 时出现 XMLHttpRequest 错误
Posted
技术标签:
【中文标题】错误:使用天气 API 时出现 XMLHttpRequest 错误【英文标题】:ERROR : XMLHttpRequest error in flutter while using weather API 【发布时间】:2022-01-16 19:06:24 【问题描述】:这里我使用天气 API。在进行调试时会出现错误,
错误:XMLHttpRequest 错误。 C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 908:28 获取当前 包/http/src/browser_client.dart 69:22 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 1232:7 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 37332:58 在 Object.createErrorWithStack (http://localhost:51980/dart_sdk.js:5388:12) 在 Object._rethrow (http://localhost:51980/dart_sdk.js:40987:16) 在 async._AsyncCallbackEntry.new.callback (http://localhost:51980/dart_sdk.js:40981:13) 在 Object._microtaskLoop (http://localhost:51980/dart_sdk.js:40808:13) 在 _startMicrotaskLoop (http://localhost:51980/dart_sdk.js:40814:13) 在 http://localhost:51980/dart_sdk.js:36279:9
我的颤振代码是:
String searchApiUrl = 'https://www.metaweather.com/api/location/search/?query=';
String locationApiUrl = 'https://www.metaweather.com/api/location/';
void fetchSearch(String input) async
var searchResult = await http.get(Uri.parse(searchApiUrl + input), headers: "Accept":
"application/json","Access-Control-Allow-Origin": "*");
var result = json.decode(searchResult.body)[0];
setState(()
location = result["title"];
woeid = result["woeid"];
);
void fetchLocation() async
var locationResult = await http.get(Uri.parse(locationApiUrl + woeid.toString()),
headers: "Accept": "application/json","Access-Control-Allow-Origin": "*");
var result = json.decode(locationResult.body);
var consolidated_weather = result["consolidated_weather"];
var data = consolidated_weather[0];
setState(()
temperature = data["the_temp"].round();
weather = data["weather_state_name"].replaceAll(' ','').toLowerCase();
);
void onTextFieldSubmitted(String input)
fetchSearch(input);
fetchLocation();
【问题讨论】:
【参考方案1】:在fetchLocation
中,您依赖于来自fetchSearch
的woeid
值,但您无需等待onTextFieldSubmitted
中的结果。试试这个:
void onTextFieldSubmitted(String input) async
await fetchSearch(input);
await fetchLocation(); // await in this line might not be needed
【讨论】:
以上是关于错误:使用天气 API 时出现 XMLHttpRequest 错误的主要内容,如果未能解决你的问题,请参考以下文章
运行天气应用程序时出现此错误我正在使用 json 和 http 库来获取数据但我不知道为啥会出现此错误