title: How to use generics and list of generics with json serialization in Dart?
body: <p>I am developing a mobile project made with Flutter. This project need to connect to some servers for REST consumption services (GET, POST, PUT, DELETE, ...), and retrieve data as well as send data to them. The data needs to be formatted in JSON , so I decided to utilize the Json serialization library 2.0.3 for Dart with Json annotation 2.0.0 and build_runner 1.2.8; It does work just fine for basic data types like int, String and bool, as well as custom objects. But it doesn't seem to work at all for generics, like a <code>&lt;T&gt; item;</code> field for instance or a <code>List&lt;T&gt; list;</code> field.</p>
  
  <p>My intention is to add some generic fields so they can be used to return all kind of json types and structures. I managed to find a solution for the first case, by using "@JsonKey" to override fromJson and toJson, and comparing <code>&lt;T&gt;</code> with the desired type I wanted to cast it to in the method. However, I couldn't find a solution to <code>List&lt;T&gt;</code> type fields. If I try to use annotation for them, all I get is a <code>List&lt;dynamic&gt;</code> type which is useless to compare classes for casting. How do I solve my predicament? Should I stick to json_serialization or use build_value instead? Any help on this matter would be very much appreciated.</p>
  
  <p>My code:</p>
  
  <pre><code>import 'package:json_annotation/json_annotation.dart';
  
  part 'json_generic.g.dart';
  
  @JsonSerializable()
  class JsonGeneric&lt;T&gt; {
    final int id;
    final String uri;
    final bool active;
    @JsonKey(fromJson: _fromGenericJson, toJson: _toGenericJson)
    final T item;
    @JsonKey(fromJson: _fromGenericJsonList, toJson: _toGenericJsonList)
    final List&lt;T&gt; list;
  
    static const String _exceptionMessage = "Incompatible type used in JsonEnvelop";
  
    JsonGeneric({this.id, this.uri, this.active, this.item, this.list});
  
    factory JsonGeneric.fromJson(Map&lt;String, dynamic&gt; json) =&gt;
        _$JsonGenericFromJson(json);
  
    Map&lt;String, dynamic&gt; toJson() =&gt; _$JsonGenericToJson(this);
  
    static T _fromGenericJson&lt;T&gt;(Map&lt;String, dynamic&gt; json) {
      if (T == User) {
        return json == null ? null : User.fromJson(json) as T;
      } else if (T == Company) {
        return json == null ? null : Company.fromJson(json) as T;
      } else if (T == Data) {
        return json == null ? null : Data.fromJson(json) as T;
      } else {
        throw Exception(_exceptionMessage);
      }
    }
  
    static Map&lt;String, dynamic&gt; _toGenericJson&lt;T&gt;(T value) {
      if (T == User) {
        return (T as User).toJson();
      } else if(T == Company) {
        return (T as Company).toJson();
      } else if(T == Data) {
        return (T as Data).toJson();
      } else {
        throw Exception(_exceptionMessage);
      }
    }
  
    static dynamic _fromGenericJsonList&lt;T&gt;(List&lt;dynamic&gt; json) {
      if (T == User) {
  
      } else if(T == Company) {
  
      } else if(T == Data) {
  
      } else {
        throw Exception(_exceptionMessage);
      }
    }
  
    static List&lt;Map&lt;String, dynamic&gt;&gt; _toGenericJsonList&lt;T&gt;(dynamic value) {
      if (T == User) {
  
      } else if(T == Company) {
  
      } else if(T == Data) {
  
      } else {
        throw Exception(_exceptionMessage);
      }
    }
  }
  </code></pre>
  
  <p>I expected to be able to serialize/deserialize "final List list;" either with "@JsonKey" or without it, but so far, I failed to find a way to cast it into the proper json format.</p>
  
  <p>When I try to generate code for this class (with the command "flutter packages pub run build_runner build"), I end up receiving the following error:</p>
  
  <p>Error running JsonSerializableGenerator
  Could not generate <code>fromJson</code> code for <code>list</code> because of type <code>T</code>.
  None of the provided <code>TypeHelper</code> instances support the defined type.
  package:json_generic.dart:11:17</p>
  
  <pre><code>   ╷
  11 │   final List&lt;T&gt; list;
     │                 ^^^^
     ╵
  </code></pre>