Reputation: 1
here i use a weather API. while do debugging it comes with an error as,
Error: XMLHttpRequest error. C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 908:28 get current packages/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 at Object.createErrorWithStack (http://localhost:51980/dart_sdk.js:5388:12) at Object._rethrow (http://localhost:51980/dart_sdk.js:40987:16) at async._AsyncCallbackEntry.new.callback (http://localhost:51980/dart_sdk.js:40981:13) at Object._microtaskLoop (http://localhost:51980/dart_sdk.js:40808:13) at _startMicrotaskLoop (http://localhost:51980/dart_sdk.js:40814:13) at http://localhost:51980/dart_sdk.js:36279:9
my flutter code is:
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();
}
Upvotes: 0
Views: 673
Reputation: 9789
In fetchLocation
you are depending on the woeid
value coming from fetchSearch
, but you do not wait for the result in onTextFieldSubmitted
. Try this:
void onTextFieldSubmitted(String input) async {
await fetchSearch(input);
await fetchLocation(); // await in this line might not be needed
}
Upvotes: 0