dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

Different approach for handling generic fields

Open HosamHasanRamadan opened this issue 1 year ago • 1 comments

I want to thank you first for this amazing package and your other contributions to the community

Handling generics in dart_mappable has so many moving parts, which isn't straight forward to configure.

But for the sake of simplicity, we need to have a generic function delegate for the generated mapper like json_serializable here

BaseResponse<T> _$BaseResponseFromJson<T extends Object?>(
  Map<String, dynamic> json,
  T Function(Object? json) fromJsonT,
) =>
    BaseResponse<T>(
      code: json['code'] as int,
      data: fromJsonT(json['data']),
    );

Map<String, dynamic> _$BaseResponseToJson<T extends Object?>(
  BaseResponse<T> instance,
  Object? Function(T value) toJsonT,
) =>
    <String, dynamic>{
      'code': instance.code,
      'data': toJsonT(instance.data),
    };

I don't want to replace the current approach for this one, it is nice to have both if possible. Current approach is way better when you heavily rely on generics.

HosamHasanRamadan avatar Mar 31 '24 09:03 HosamHasanRamadan

Hi, I'm not sure what you are proposing.

dart_mappable handles generics automatically, you don't need to provide a helper function as with json_serializable. I don't see what value adding this would bring, as it would just be a step back in my opinion.

schultek avatar Mar 31 '24 13:03 schultek