What is the best way to use json_serializable with generics?

239 views Asked by At

I'm trying to achieve something like this without a lot of higher-level codes, and customizations. Will be okay if the solution works really well on cases of deeply nested T values, even if it involves writing a lot of higher-level and customized codes.

@JsonSerializable()
class Response<T> {  // T can be every type from native types, to nested generic custom classes
    final int code;
    final T data;

    Response({
        this.code,
        this.data,
    });
}

I've tried a few solutions that involve writing a custom type converter for that generic part, which of course doesn't work with deeply nested values of T.

0

There are 0 answers