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

The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>'

final dataResponse = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
    Album.fromJson(jsonDecode(dataResponse.body));

On nullsafety enabled project, Album.fromJson(jsonDecode(dataResponse.body)); this code throwing error The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>'.

Followed official doc

Following code used for data modeling.

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

class Album {
  final int userId;
  final int id;
  final String title;

  Album({
    required this.userId,
    required this.id,
    required this.title,
  });

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      userId: json['userId'],
      id: json['id'],
      title: json['title'],
    );
  }
}

>Solution :

You just need to cast your jsonDecode

Album.fromJson(jsonDecode(dataResponse.body));

to explicit type:

Album.fromJson(jsonDecode(dataResponse.body) as Map<String, dynamic>);

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