COD 400,消息:错误的纬度,开放服:获取当前的WeatherData

发布于 2025-01-21 07:19:27 字数 1138 浏览 4 评论 0原文

当我试图获取当前天气数据时,我会遇到错误(纬度错误)。谁能告诉我出了什么问题 我尝试将Location Accuracy从低点更改为最佳,但并没有改变任何

Location location = Location();
class _LoadingScreenState extends State<LoadingScreen> {
  String weatherABI = '4ead5f41d1c622302dd34242f9d1c25a';
  void getlocation() async {
    await location.getCurrentLocation();
    print(location.latitude);
    print(location.longtiude);
  }

  void getData() async {
    Uri url = Uri.parse(
        'https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longtiude}&appid=$weatherABI');
    Response response = await get(url);
    print(response.body);

    if (response.statusCode == 200) {
      String data = response.body;
      print(data);
    } else {
      print(response.statusCode);
    }
  }

  @override
  void initState() {
    super.initState();
    getlocation();
    getData();
  }

  Widget build(BuildContext context) {
    return Scaffold();
  }
}

错误:

I'm getting error (wrong latitude) when I'm trying to get current weather data. can anyone tell me what went wrong
I tried changing locationAccuracy from low to best but it didn't change anything

Location location = Location();
class _LoadingScreenState extends State<LoadingScreen> {
  String weatherABI = '4ead5f41d1c622302dd34242f9d1c25a';
  void getlocation() async {
    await location.getCurrentLocation();
    print(location.latitude);
    print(location.longtiude);
  }

  void getData() async {
    Uri url = Uri.parse(
        'https://api.openweathermap.org/data/2.5/weather?lat=${location.latitude}&lon=${location.longtiude}&appid=$weatherABI');
    Response response = await get(url);
    print(response.body);

    if (response.statusCode == 200) {
      String data = response.body;
      print(data);
    } else {
      print(response.statusCode);
    }
  }

  @override
  void initState() {
    super.initState();
    getlocation();
    getData();
  }

  Widget build(BuildContext context) {
    return Scaffold();
  }
}

the error :
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2025-01-28 07:19:27

只需在 getLocation()函数末尾调用您的 getData()函数,因为两个函数同时调用,这就是为什么 getData()函数的原因没有获得任何位置数据。您需要先获取位置,然后需要调用 getData 功能。

删除 getData()函数从 initstate()

  void getlocation() async {
   await location.getCurrentLocation();
   getData();
   print(location.latitude);
   print(location.longtiude);
  }

您还需要在 getData()函数中调用SetState(一旦从API检索数据)以更新您的UI。

Just call your getData() function in the end of getLocation() function because both functions called at same time that's why getData() function does not get any location data. You need to get location first then you need to call getData function.

Remove getData() function from initState()

  void getlocation() async {
   await location.getCurrentLocation();
   getData();
   print(location.latitude);
   print(location.longtiude);
  }

You also need to call setState in getData() function once you retrieved data from the API to update your UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文