拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何修复类名的错误实体?

如何修复类名的错误实体?

白鹭 - 2022-03-25 2131 0 0

添加覆写 toSting() 后,输出如下所示..

I/flutter (25471): [London11.0811.741012815.3]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93]

我只需要遍历每个项目的地名、温度、湿度、风速、压力和感觉。我在打印输出时做错了什么?

在预测资料中

import 'package:forecast/models/weather_data.dart';

class ForecastData {
  final List list;

  ForecastData({required this.list});

  factory ForecastData.fromJson(Map<String, dynamic> json) {
    var list = json['list']?.map((e) => e)?.toList(growable: true) ?? [];
    List weatherData = [];
    list.forEach((e) {
      WeatherData w = WeatherData(
          placeName: json['city']['name'],
          temperature: e['main']['temp'],
          feels_like: e['main']["feels_like"],
          pressure: e['main']['pressure'],
          humidity: e['main']['humidity'],
          wind_speed: e['wind']['speed']);

      weatherData.add(w);
      print(weatherData.toList());
    });

    return ForecastData(list: weatherData);
  }
}

在weatherdata.dart中

class WeatherData {
  String? placeName;
  num? temperature;
  num? feels_like;
  num? pressure;
  num? humidity;
  num? wind_speed;

  WeatherData({
    this.placeName,
    this.temperature,
    this.feels_like,
    this.pressure,
    this.humidity,
    this.wind_speed,
  });
  @override
  String toString() =>
      '${placeName ?? 'unknown'}${feels_like ?? 'unknown'}${temperature ?? 'unknown'}${pressure ?? 'unknown'}${humidity ?? 'unknown'}${wind_speed ?? 'unknown'}';

  WeatherData.fromJson(Map<String, dynamic> json) {
    placeName = json['city']['name'];
    temperature = json['list'][0]['main']['temp'];
    feels_like = json['list'][0]['main']['temp'];
    pressure = json['list'][0]['main']['temp'];
    humidity = json['list'][0]['main']['temp'];
    wind_speed = json['list'][0]['main']['temp'];
  }
}

json

{
  "cod": "200",
  "message": 0,
  "cnt": 40,
  "list": [
    {
      "dt": 1641124800,
      "main": {
        "temp": 11.74,
        "feels_like": 11.08,
        "temp_min": 11.74,
        "temp_max": 12,
        "pressure": 1012,
        "sea_level": 1012,
        "grnd_level": 1008,
        "humidity": 81,
        "temp_kf": -0.26
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 75
      },
      "wind": {
        "speed": 5.3,
        "deg": 229,
        "gust": 11.75
      },
      "visibility": 10000,
      "pop": 0,
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2022-01-02 12:00:00"
    },
    {
      "dt": 1641135600,
      "main": {
        "temp": 11.86,
        "feels_like": 11.11,
        "temp_min": 11.86,
        "temp_max": 12.09,
        "pressure": 1011,
        "sea_level": 1011,
        "grnd_level": 1006,
        "humidity": 77,
        "temp_kf": -0.23
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 83
      },
      "wind": {
        "speed": 6.45,
        "deg": 223,
        "gust": 15.9
      },
      "visibility": 10000,
      "pop": 0.7,
      "rain": {
        "3h": 0.6
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2022-01-02 15:00:00"
    },
{
      "dt": 1641546000,
      "main": {
        "temp": 3.96,
        "feels_like": 0.41,
        "temp_min": 3.96,
        "temp_max": 3.96,
        "pressure": 1010,
        "sea_level": 1010,
        "grnd_level": 1007,
        "humidity": 82,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 100
      },
      "wind": {
        "speed": 4.29,
        "deg": 239,
        "gust": 11.29
      },
      "visibility": 10000,
      "pop": 0.02,
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2022-01-07 09:00:00"
    }
  ],
  "city": {
    "id": 2643743,
    "name": "London",
    "coord": {
      "lat": 51.5085,
      "lon": -0.1257
    },
    "country": "GB",
    "population": 1000000,
    "timezone": 0,
    "sunrise": 1641110761,
    "sunset": 1641139355
  }
}

有人能告诉我我是否正确地迭代了forecast.data 中的每个项目吗?以及如何解决它?

uj5u.com热心网友回复:

return placeName ?? '';

你只需要处理它是否为空

uj5u.com热心网友回复:

输入字符串?意味着它必须在这种型别的变量中具有字符串或空值所以解决方案是将型别更改为字符串并将所需的关键字添加到建构式中的命名自变量中。试试这可能有用...

uj5u.com热心网友回复:

//'A value of type 'String?' can't be returned from the method 'toString' because it has a return type of 'String'

显示这一点是因为您必须在那里处理空例外。placeNmae?

if (placeName != null) {
    return placeName;
  //2
  } else {
    codeExceptionsomething();
  }
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *