Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Model class not recognizing return arguments

I am creating a model class for managing the results given by Googles Places API:

class PlaceSearch {

  final String description;
  final String placeId;

  PlaceSearch(this.description, this.placeId);

  factory PlaceSearch.fromJson(Map<String, dynamic> json){

    return PlaceSearch(description: json['description'], placeId: json['place_id']);

  }
}

But I am getting an error at line

return PlaceSearch(description: json['description'], placeId: json['place_id']);

The error is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2 positional argument(s) expected, but 0 found

That class has been used in an older Flutter project, but now it is not working.

What should I change?

>Solution :

You aren’t using named constructor on PlaceSearch(this.description, this.placeId);

class PlaceSearch {
  final String description;
  final String placeId;

  PlaceSearch({required this.description, required this.placeId});

  factory PlaceSearch.fromJson(Map<String, dynamic> json) {
    return PlaceSearch(
        description: json['description'], placeId: json['place_id']);
  }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading