'The named parameter 'time' is required, but there's no corresponding argument. Try adding the required argument flutter problem?

In order to get weather Data using OpenWeatherMap API, I created a Weather class as shown in the code below:

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';

const appId = "d8cdd9eca073d9bbf5ef49405cbf50e8";

class Weather {
  final int max;
  final int min;
  final int current;
  final String name;
  final String day;
  final int wind;
  final int humidity;
  final int chanceRain;
  final String image;
  final String time;
  final String location;

  Weather(
      {
        required this.max,
     required this.min,
     required this.name,
    required  this.day,
    required  this.wind,
     required this.humidity,
    required  this.chanceRain,
     required this.image,
    required  this.current,
     required this.time,
    required  this.location
    });
}

Then, I created the method fetchData to get the current temp, today & tomorrow weather and 7 day weather as shown below:

Future<List> fetchData(String lat,String lon,String city) async{
  var url = "https://api.openweathermap.org/data/2.5/onecall?lat=$lat&lon=$lon&units=metric&appid=$appId";
  var response = await http.get(Uri.parse(url));
  DateTime date = DateTime.now();
  if(response.statusCode==200){
    var res = json.decode(response.body);
    //current Temp
    var current = res["current"];
    Weather currentTemp =  Weather(
      current: current["temp"]?.round()??0,
      name: current["weather"][0]["main"].toString(),
      day: DateFormat("EEEE dd MMMM").format(date),
      wind: current["wind_speed"]?.round()??0,
      humidity: current["humidity"]?.round()??0,
      chanceRain: current["uvi"]?.round()??0,
      location: city,
      image: findIcon(current["weather"][0]["main"].toString(), true)
    );
  
    //today weather
    List<Weather> todayWeather = [];
    int hour = int.parse(DateFormat("hh").format(date));
    for(var i=0;i<4;i++){
      var temp = res["hourly"];
      var hourly = Weather(
        current: temp[i]["temp"]?.round()??0,
        image: findIcon(temp[i]["weather"][0]["main"].toString(),false),
        time: Duration(hours: hour+i+1).toString().split(":")[0]+":00"
      );
      todayWeather.add(hourly);
    }

    //Tomorrow Weather
    var daily = res["daily"][0];
    Weather tomorrowTemp = Weather(
      max: daily["temp"]["max"]?.round()??0,
      min:daily["temp"]["min"]?.round()??0,
      image: findIcon(daily["weather"][0]["main"].toString(), true),
      name:daily["weather"][0]["main"].toString(),
      wind: daily["wind_speed"]?.round()??0,
      humidity: daily["rain"]?.round()??0,
      chanceRain: daily["uvi"]?.round()??0
    );

    //Seven Day Weather
    List<Weather> sevenDay = [];
    for(var i=1;i<8;i++){
      String day = DateFormat("EEEE").format(DateTime(date.year,date.month,date.day+i+1)).substring(0,3);
      var temp = res["daily"][i];
      var hourly = Weather(
        max:temp["temp"]["max"]?.round()??0,
        min:temp["temp"]["min"]?.round()??0,
        image:findIcon(temp["weather"][0]["main"].toString(), false),
        name:temp["weather"][0]["main"].toString(),
        day: day
      );
      sevenDay.add(hourly);
    }
    return [currentTemp,todayWeather,tomorrowTemp,sevenDay];
  }
  return [null,null,null,null];
}

I got the following problems:

  1. The named parameter 'time' is required, but there's no corresponding argument. Try adding the required argument.

  2. The named parameter 'max' is required, but there's no corresponding argument. Try adding the required argument.

  3. The named parameter 'day' is required, but there's no corresponding argument. Try adding the required argument. And the round() function seems like it doesn't work!



Solution 1:[1]

If you mark those as required, you need to provide any time you use the Weather(...) constructor, you can't ignore those parameters.

If you want to have those as optional, you need to remove the required keyword, but you get the error because a non-nullable value can't be null.

In order to have a nullable value you need to postpone a ? at the end of your variable type for example

String -> non nullable String

String? -> nullable String

so if you want to be able to define only some of the parameter you need to change your class this way

class Weather {
  final int? max;
  final int? min;
  final int? current;
  final String? name;
  final String? day;
  final int? wind;
  final int? humidity;
  final int? chanceRain;
  final String? image;
  final String? time;
  final String? location;

  Weather(
      {
        this.max,
        this.min,
        this.name,
        this.day,
        this.wind,
        this.humidity,
        this.chanceRain,
        this.image,
        this.current,
        this.time,
        this.location,
      });
}

Of course this will be not enough, because you may want to have either optional and required parameters, but you can discover more about sound null safety here: https://dart.dev/null-safety

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CLucera