Json_serializable.dart: Generics generation error

Created on 3 Jul 2018  路  8Comments  路  Source: google/json_serializable.dart

I'm trying to generate a file with generic inside and can't figure how to make it work :/

Here is my file:

import 'package:json_annotation/json_annotation.dart';

part 'paginated_list.g.dart';

@JsonSerializable()
class PaginatedList<T> extends Object with _$PaginatedListSerializerMixin {
  PaginatedList(this.count, this.next, this.previous, this.results);

  int count ;
  String next;
  String previous;
  List<T> results;

  bool hasNext() {
    return next != null && !next.isEmpty;
  }
  factory PaginatedList.fromJson(Map<String, dynamic> json) => _$PaginatedListFromJson(json);
}

And it fails with:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'paginated_list.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

// Error: Could not generate `fromJson` code for `results` because of type `T`.
//        None of the provided `TypeHelper` instances support the defined type.
//        package:flutter_app/models/paginated_list.dart:20:11
//          List<T> results;
//                  ^^^^^^^
// TODO: Make sure all of the types are serializable.

I'm using:
json_annotation: "^0.2.9"
build_runner: "0.8.10"
json_serializable: "^0.5.8"

Any idea what am I doing wrong ?

Most helpful comment

All 8 comments

I am getting the same error, but I am using T data; instead of List<T> results;.

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'response_data.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

// Error: Could not generate `fromJson` code for `data`.
//        None of the provided `TypeHelper` instances support the defined type.
//        package:flutter_app/model/response_data.dart:8:11
//          final T data;
//                  ^^^^
// TODO: Make sure all of the types are serializable.

+1
[SEVERE] json_serializable:json_serializable on lib/model/user.dart:
Error running JsonSerializableGenerator
Could not generate toJson code for cmndInfo.
None of the provided TypeHelper instances support the defined type.

Try using toJson/fromJson on the JsonKey annotation

Is it possible to transform a nested generic type? Something like this:

T _dataFromJson<T>(Map<String, dynamic> input) {
  return T.fromJson(input);
} 

Is it possible to transform a nested generic type? Something like this:

T _dataFromJson<T>(Map<String, dynamic> input) {
  return T.fromJson(input);
} 

It is NOT, sadly. If you can figure out T from the contents of input you can do something like.

T _dataFromJson<T>(Map<String, dynamic> input) {
  if (input.containsKey('something')) {
    return Something.fromJson(input) as T;
  }
  throw UnsupportedException('???');
}

I'm trying to generate a file with generic inside and can't figure how to make it work :/

Here is my file:

import 'package:json_annotation/json_annotation.dart';

part 'paginated_list.g.dart';

@JsonSerializable()
class PaginatedList<T> extends Object with _$PaginatedListSerializerMixin {
  PaginatedList(this.count, this.next, this.previous, this.results);

  int count ;
  String next;
  String previous;
  List<T> results;

  bool hasNext() {
    return next != null && !next.isEmpty;
  }
  factory PaginatedList.fromJson(Map<String, dynamic> json) => _$PaginatedListFromJson(json);
}

And it fails with:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'paginated_list.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

// Error: Could not generate `fromJson` code for `results` because of type `T`.
//        None of the provided `TypeHelper` instances support the defined type.
//        package:flutter_app/models/paginated_list.dart:20:11
//          List<T> results;
//                  ^^^^^^^
// TODO: Make sure all of the types are serializable.

I'm using:
json_annotation: "^0.2.9"
build_runner: "0.8.10"
json_serializable: "^0.5.8"

Any idea what am I doing wrong ?

@jaumard were you able to solve this issue? what did you do

I used jaguar_serializer instead of json_serializable ^^

Was this page helpful?
0 / 5 - 0 ratings