ОШИБКА: ошибка XMLHttpRequest во флаттере при использовании API погоды

здесь я использую погодный 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 runUnaryC: / b / s / w / ir / cache / builder / src / out / host_debug / dart-sdk / lib / async / future_impl.dart 160:18 handleValueC: / b / s / w / ir / cache / builder / src / out / host_debug / dart-sdk / lib / async / future_impl.dart 767:44 handleValueCallbackC:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 796:13 _propagateToListenersC: / 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 _cancelAndValueC:/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 _checkAndCallC: / 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вы находитесь в зависимости от woeidзначение, исходящее от fetchSearch, но вы не ждете результата в onTextFieldSubmitted. Попробуй это:

      void onTextFieldSubmitted(String input) async {
    await fetchSearch(input);
    await fetchLocation(); // await in this line might not be needed
}
Другие вопросы по тегам