Home > Software design >  when I execute the code the result it give "null" in Temp & in minTemp
when I execute the code the result it give "null" in Temp & in minTemp

Time:08-26

this the code

class WeatherModel {
      final String? date;
      final double? temp;
      final double? maxTemp;
      final double? minTemp;
      final String? weatherStateName;
      WeatherModel(
          {required this.date,
          required this.temp,
          required this.maxTemp,
          required this.minTemp,
          required this.weatherStateName});
      factory WeatherModel.fromJson(dynamic data) {
        var jsonData = data['forecast']?['forecastday']?[0]?['day'];
        print('$jsonData');
        return WeatherModel(
            date: data['location']?['localtime'],
            temp: jsonData?['avgtemp_c'],
            maxTemp: jsonData?['maxtemp_c'],
            minTemp: jsonData?['mintemp_c'],
            weatherStateName: jsonData?['condition']?['text']);
      }
      @override
      String toString() {
        return 'tem = $temp minTemp = $minTemp date = $date';
      }
    }

this the image [https://i.stack.imgur.com/T6HV9.png]

this the result when I execute the code

I/flutter ( 5213): tem = null minTemp = null date = 2022-08-25 20:26

CodePudding user response:

As I can see in picture you publish, there also result of 21 line:

print('$jsonData');

is null. This means that $jsonData is null. That's why in $temp and $minTemp variables also null. Please check the structure of JSON. You can print data, to see it.

  • Related