代码之家  ›  专栏  ›  技术社区  ›  mohitnx

快照hasData即使响应也返回false。身体有数据[颤振]

  •  0
  • mohitnx  · 技术社区  · 4 年前

    我正在尝试从OpenWeather获取当前天气信息,并在检查返回的数据后在文本小部件中显示结果。但是,该条件(snapshot.hasData)始终返回为false,否则将调用条件(CircularProgressIndicator)。 这是FutureBuilder。

    class WeatherPage extends StatefulWidget {
      @override
      State<WeatherPage> createState() => _WeatherPageState();
    }
    
    class _WeatherPageState extends State<WeatherPage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: FutureBuilder(
              future: getCurrentWeather(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  WeatherModelCurrent weather =
                      snapshot.data as WeatherModelCurrent;
    
                  return weatherBox(weather);
                } else {
                  return const CircularProgressIndicator();
                }
              },
            ),
          ),
        );
      }
    

    下面是getCurrentWeather()函数

    Future getCurrentWeather() async {
        WeatherModelCurrent? weather;
    
        var url = Uri.parse(
            "https://api.openweathermap.org/data/2.5/weather?q=Kathmandu&appid=82c7b99e2a8215351147f607592a3e63&units=metric");
        var response = await http.get(url);
    //response.body has the data returned by API call
        print(response.body);
    
        if (response.statusCode == 200) {
          weather = WeatherModelCurrent.frommJson(jsonDecode(response.body));
        
        } else {
          print('error');
        }
    
        return weather;
      }
    

    这是模型课

    class WeatherModelCurrent {
      double temp;
      double feelslike;
      double tempmin;
      double tempmax;
      String description;
    
      WeatherModelCurrent(
          {required this.temp,
          required this.feelslike,
          required this.tempmin,
          required this.tempmax,
          required this.description});
    
      factory WeatherModelCurrent.frommJson(Map<String, dynamic> jsonn) {
        return WeatherModelCurrent(
          temp: jsonn['main']['temp'].toDouble(),
          feelslike: jsonn['main']['feels_like'].toDouble(),
          tempmin: jsonn['main']['temp_min'].toDouble(),
          tempmax: jsonn['main']['temp_max'].toDouble(),
          description: jsonn['weather']['description'],
        );
      }
    }
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   Lebecca    4 年前

    尝试(&T);抓住你的 getCurrentWeather 代码块,我们可以得到以下异常:

    flutter: exception type 'String' is not a subtype of type 'int' of 'index'
    

    响应json类似于:

    {
        "weather":[
            {
                "id":803,
                "main":"Clouds",
                "description":"broken clouds",
                "icon":"04d"
            }
        ],
    }
    

    您的代码 description: jsonn['weather']['description'] 应该是 description: jsonn['weather'][0]['description'],

        2
  •  0
  •   Samu Chakraborty    4 年前

    出现错误是因为您分析了响应。身体处于错误的等级。 试试这个模型课

     class WeatherModelCurrent {
      WeatherModelCurrent({
    this.coord,
    this.weather,
    this.base,
    this.main,
    this.visibility,
    this.wind,
    this.clouds,
    this.dt,
    this.sys,
    this.timezone,
    this.id,
    this.name,
    this.cod,
     });
    
     final Coord? coord;
     final List<Weather>? weather;
     final String? base;
    final Main? main;
     final int? visibility;
     final Wind? wind;
     final Clouds? clouds;
     final int? dt;
     final Sys? sys;
     final int? timezone;
     final int? id;
     final String? name;
       final int? cod;
    
         factory WeatherModelCurrent.fromJson(Map<String, dynamic> json) =>  WeatherModelCurrent(
    coord: Coord.fromJson(json["coord"]),
    weather: List<Weather>.from(json["weather"].map((x) =>   Weather.fromJson(x))),
    base: json["base"],
    main: Main.fromJson(json["main"]),
    visibility: json["visibility"],
    wind: Wind.fromJson(json["wind"]),
    clouds: Clouds.fromJson(json["clouds"]),
    dt: json["dt"],
    sys: Sys.fromJson(json["sys"]),
    timezone: json["timezone"],
    id: json["id"],
    name: json["name"],
    cod: json["cod"],
      );
    
      Map<String, dynamic> toJson() => {
      "coord": coord!.toJson(),
    "weather": List<dynamic>.from(weather!.map((x) => x.toJson())),
    "base": base,
    "main": main!.toJson(),
    "visibility": visibility,
    "wind": wind!.toJson(),
    "clouds": clouds!.toJson(),
    "dt": dt,
    "sys": sys!.toJson(),
    "timezone": timezone,
    "id": id,
    "name": name,
    "cod": cod,
    };
    }
    
    class Clouds {
     Clouds({
    this.all,
    });
    
    final int? all;
    
     factory Clouds.fromJson(Map<String, dynamic> json) => Clouds(
    all: json["all"],
     );
    
     Map<String, dynamic> toJson() => {
     "all": all,
      };
     }
    
      class Coord {
     Coord({
    this.lon,
    this.lat,
    });
    
    final double? lon;
     final double? lat;
    
     factory Coord.fromJson(Map<String, dynamic> json) => Coord(
      lon: json["lon"].toDouble(),
      lat: json["lat"].toDouble(),
    );
    
    Map<String, dynamic> toJson() => {
    "lon": lon,
    "lat": lat,
     };
      }
    
      class Main {
    Main({
    this.temp,
    this.feelsLike,
    this.tempMin,
    this.tempMax,
    this.pressure,
    this.humidity,
     });
    
     final double? temp;
    final double? feelsLike;
    final double? tempMin;
    final double? tempMax;
    final int? pressure;
    final int? humidity;
    
    factory Main.fromJson(Map<String, dynamic> json) => Main(
    temp: json["temp"].toDouble(),
    feelsLike: json["feels_like"].toDouble(),
    tempMin: json["temp_min"].toDouble(),
    tempMax: json["temp_max"].toDouble(),
    pressure: json["pressure"],
    humidity: json["humidity"],
    );
    
    Map<String, dynamic> toJson() => {
    "temp": temp,
    "feels_like": feelsLike,
    "temp_min": tempMin,
    "temp_max": tempMax,
    "pressure": pressure,
    "humidity": humidity,
      };
        }
    
     class Sys {
     Sys({
    this.type,
    this.id,
    this.country,
    this.sunrise,
    this.sunset,
     });
    
     final int? type;
     final int? id;
    final String? country;
    final int? sunrise;
     final int? sunset;
    
     factory Sys.fromJson(Map<String, dynamic> json) => Sys(
    type: json["type"],
    id: json["id"],
    country: json["country"],
    sunrise: json["sunrise"],
    sunset: json["sunset"],
     );
    
     Map<String, dynamic> toJson() => {
    "type": type,
    "id": id,
    "country": country,
    "sunrise": sunrise,
    "sunset": sunset,
     };
      }
    
      class Weather {
      Weather({
    this.id,
    this.main,
    this.description,
    this.icon,
    });
    
    final int? id;
    final String? main;
    final String? description;
    final String? icon;
    
     factory Weather.fromJson(Map<String, dynamic> json) => Weather(
      id: json["id"],
      main: json["main"],
      description: json["description"],
      icon: json["icon"],
    );
    
    Map<String, dynamic> toJson() => {
    "id": id,
    "main": main,
    "description": description,
    "icon": icon,
     };
    }
    
      class Wind {
      Wind({
    this.speed,
    this.deg,
    });
    
    final double? speed;
    final int? deg;
    
     factory Wind.fromJson(Map<String, dynamic> json) => Wind(
      speed: json["speed"].toDouble(),
      deg: json["deg"],
       );
    
    Map<String, dynamic> toJson() => {
      "speed": speed,
      "deg": deg,
    };
      }