snapshot.hasdata返回false,即使响应也有数据[flutter]

发布于 2025-02-12 10:02:59 字数 2207 浏览 0 评论 0原文

我正在尝试从OpenWeather获取当前的天气信息,并在检查返回数据后在文本小部件中显示结果。但是,始终将Condtion(Snapshot.hasdata)返回为false,否则调用条件(循环刺激器)。 这是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'],
    );
  }
}

I am trying to get current weather info from OpenWeather and display the result in Text Widget after checking that there is data returned. However, that condtion (snapshot.hasData) is always returned as false and else condition (CircularProgressIndicator) is invoked.
Here is the 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();
            }
          },
        ),
      ),
    );
  }

here is the getCurrentWeather() function

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;
  }

and here is the model class

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'],
    );
  }
}

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

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

发布评论

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

评论(2

江心雾 2025-02-19 10:02:59

尝试&amp;捕获您的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 ['ateathy'] ['description'] ['description']应该是描述:JSONN ['天气'] [0] ['Description'],

Try & catch your getCurrentWeather code block, we can get the following exception:

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

The response json is like:

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

Your code description: jsonn['weather']['description'] should be description: jsonn['weather'][0]['description'],

半﹌身腐败 2025-02-19 10:02:59

发生错误是因为您将响应解析为错误的班级。
尝试此模型类

 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,
};
  }

The error is occur because you parse your response.body in wrong class.
Try this model class

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