【问题标题】:Getting weather of some other location, even after passing correct latitude and longitude获取其他位置的天气,即使经过正确的纬度和经度
【发布时间】:2020-08-12 14:17:56
【问题描述】:

我正在使用这个需要纬度和经度才能获取天气的天气 API。我能够获得我所在位置的纬度和经度(尝试打印它们并且它们是正确的)但是当我在我的 API 链接中输入它们时,它会显示其他纬度和经度的天气。我已经尝试将我的位置的经纬度手动放入链接中,它工作得非常好。有什么问题?

import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

double key;

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  var lati;
  var longi;

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

  void getData() async {
    http.Response r = await http.get('http://api.weatherstack.c'
        'om/current?access_key=41eb36e0c5f82e3ddce66ef01af877a1&query=$lati,$longi');
    String s = r.body;
    var data1 = jsonDecode(s)['location']['lat'];
    var data2 = jsonDecode(s)['location']['lon'];
    print(data1);
    print(data2);
    print(lati);
    print(longi);
  }

  void getlocation() async {
    location a = location();
    await a.getclocation();
    lati = a.lat;
    longi = a.long;
  }

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

另一个名为 location 的类

import 'package:geolocator/geolocator.dart';
class location{

  double lat;
  double long;


  Future<void> getclocation() async{
// this function is defined in another class
   Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
  lat=position.latitude;
  long=position.longitude;


    }



}

输出是

41.250

1.300

28.7041

77.1025

【问题讨论】:

  • 你能打印这个吗:var query = jsonDecode(s)['request']['query'];
  • null,null 是输出
  • 检查我的答案

标签: flutter dart


【解决方案1】:

您需要等到您的操作真正完成。

有关 Future 工作原理的更广泛解释,请参阅What is a Future and how do I use it?

如果你有一个async 函数,让它返回一个Future&lt;T&gt;,即使它是一个Future&lt;void&gt;。然后确保你await它如果你需要它的结果。在 UI 中,您不能只是停止并冻结所有内容,您需要一个 FutureBuilder 才能在等待结果时播放得很好并显示一些动画。

class _LoadingScreenState extends State<LoadingScreen> {
  Future<String> getDataFuture;

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

  Future<String> getData() async {
    final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
    final r = await http.get('http://api.weatherstack.com/current?access_key=41eb36e0c5f82e3ddce66ef01af877a1&query=$position.latitude,$position.longitude');
    return r.body;
  }

  Widget build(BuildContext context) {
    return FutureBuilder(
              future: getDataFuture ,
              builder: (context, snapshot) {
                if(snapshot.hasData) {
                  return Text('Received response: ${snapshot.data}');
                } else if(snapshot.hasError) {
                  return Text('Error: ${snapshot.error.toString()}');
                } else {
                  return CircularProgressIndicator();
                }
  }
}

【讨论】:

    猜你喜欢
    • 2011-10-22
    • 2013-02-02
    • 1970-01-01
    • 2011-07-03
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2018-11-08
    相关资源
    最近更新 更多