在这里,我使用天气 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:
我的颤振代码是:
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();
}