import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:json_annotation/json_annotation.dart';
part 'http.g.dart';
@JsonSerializable()
class Album {
final int userId;
final int id;
final String title;
Album({this.userId, this.id, this.title});
factory Album.fromJson(Map<String, dynamic> json) => _$AlbumFromJson(json);
Map<String, dynamic> toJson() => _$AlbumToJson(this);
}
Future<Album> fetchAlbum() async {
var resp = await http.get('https://jsonplaceholder.typicode.com/albums/1');
var A = Album.fromJson(jsonDecode(resp.body)); // Error line
print(A.title);
print(A.toJson());
}
Error - At var A = Album.fromJson(jsonDecode(resp.body)); is The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>'.dartargument_type_not_assignable.
jsonDecode return type is dynamic whereas Album factory method expects a Map<String, dynamic>.
It shows compile-time error but I am able to get desired outputs in debug console.
How should we solve this?