Home > other >  How can I fix the error instance of class name?
How can I fix the error instance of class name?

Time:01-02

After adding override toSting(), output printed out like this..

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]

I just need to iterate over each item for place name, temp, humidity, wind spee, pressure and feels like. What am I doing wrong to get the output printed out?

in forecast.data

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

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

Can someone tell me if I'm iterating over each item correctly in forecast.data? and how to fix it?

CodePudding user response:

return placeName ?? '';

you just need to handle if its null

CodePudding user response:

Type String? means it has to have a string or null in variable of this type So the solution is to change type to String and add required keyword to named parameter in constructor. Try this might work...

CodePudding user response:

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

showing this because you've to handle null exception there. do placeNmae? or

if (placeName != null) {
    return placeName;
  //2
  } else {
    codeExceptionsomething();
  }
}
  • Related