I'm working on the ApiResponse class of my Flutter project and this class needs a T data
attribut.
I want to use JsonSerializable to generate the fromJson()
method but this error appears when I tried to :
ListResponse<T> _$ListResponseFromJson<T extends JsonSerializable>(Map<String, dynamic> json, T Function(Object?) fromJsonT)
I defined the those two files :
1. Base response
part "base_response.g.dart";
@JsonSerializable()
abstract class BaseResponse {
dynamic message;
bool success;
BaseResponse({this.message, required this.success});
factory BaseResponse.fromJson(Map<String, dynamic> json) =>
_$BaseResponseFromJson(json);
}
2. List response
part "list_response.g.dart";
@JsonSerializable(genericArgumentFactories: true)
class ListResponse<T extends JsonSerializable> extends BaseResponse {
List<T> data;
ListResponse({
required String super.message,
required super.success,
required this.data,
});
factory ListResponse.fromJson(Map<String, dynamic> json) =>
_$ListResponseFromJson(json, WHAT SHOULD BE HERE ?);
}
So my problem now is that JsonSerializable need another parameter in ListResponseFromJson()
:
How to dynamically pass the T.fromJson() to _$ListResponseFromJson(json, ???)
?