Error: Too few positional arguments: 4 required, 0 given

Viewed 56

I'm trying to create a clock app, I created a constructor for the parameters for the world clock here;

class WorldClock {

  String flag;
  String location;
  String time;
  String url;

  WorldClock(this.flag, this.location, this.url,this.time);

I called this class in the Parent class using three of these constructor here;

void setupWorldtime() async{

    WorldClock instance = WorldClock(location:'Berlin',url:'Europe/Berlin');(source of the error msg)
    await instance.getime();
    print(instance.time);
    setState(() {
      time = instance.time;
    });
  }
1 Answers

If you need to call named constructor and force the attributes to be called, it is necessary to wrap it with curly brackets and add required keyword at the beginning of each attribute.

class WorldClock {

  String flag;
  String location;
  String time;
  String url;

  WorldClock({required this.flag,required this.location,required this.url,required this.time});
}
Related