天气情况

这个应用程序主要是为了理解如何使用 API,如何从 API 获取数据(例如使用 GET 方法)而创建的。

应用截图

image1 image2 image3

在这个应用中,我使用了 https://collectapi.com 服务作为 API 服务。 您可以通过点击下面的链接来访问天气 API。

https://collectapi.com/tr/api/weather/hava-durumu-api


对于不了解的朋友,在从 API 获取数据时,您需要在 header 部分发送网站提供的 authorization

密钥,否则您将收到以下错误。

unAuthorizedError

要解决这个问题,您可以在 GET 方法中使用以下方法之一:

await http.get(Uri.parse("https://api.collectapi.com/weather/getWeather?data.lang=tr&data.city=$city"), headers: {
      HttpHeaders.authorizationHeader: 'apikey YOURAPIKEYHEREYOURAPIKEYHEREYOURAPIKEYHERE',
      HttpHeaders.contentTypeHeader: 'application/json'
    });

或者

await http.get(Uri.parse("https://api.collectapi.com/weather/getWeather?data.lang=tr&data.city=$city"), headers: {
      "authorization": 'apikey YOURAPIKEYHEREYOURAPIKEYHEREYOURAPIKEYHERE',
      "content": 'application/json'
    });

GitHub

查看 Github