json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Found more than one matching converter for `int?`.

Open jiechic opened this issue 11 months ago • 0 comments

my json convertor

  const JsonIntConverter();

  @override
  int fromJson(dynamic json) {
    if (json is int) {
      return json;
    } else {
      return int.parse(json);
    }
  }

  @override
  dynamic toJson(int object) {
    return object;
  }
}

class JsonNullIntConverter extends JsonConverter<int?, dynamic> {
  const JsonNullIntConverter();

  @override
  int? fromJson(dynamic json) {
    if (json == null) {
      return null;
    }
    if (json is int) {
      return json;
    } else {
      return int.parse(json);
    }
  }

  @override
  dynamic toJson(int? object) {
    return object;
  }
}

my entity

@JsonSerializable(converters: [const JsonNullIntConverter,const JsonIntConverter()])
class FileInfo {
  @JsonKey(name: 'status')
  int status;

  @JsonKey(name: 'file_id')
  int fileId = 0;

  @JsonKey(name: 'file_name')
  String? fileName;

  @JsonKey(name: 'file_path')
  String? filePath;

  @JsonKey(name: 'file_preview_url')
  String? filePreviewUrl;

  @JsonKey(name: 'file_size')
  int? fileSize = 0;
}

error Found more than one matching converter for `int?`.

if i remove JsonIntConverter,JsonNullIntConverter di not gen code for int

FileInfo _$FileInfoFromJson(Map<String, dynamic> json) => FileInfo(
      status: json['status'] as int? ?? 0,
      fileId: json['file_id'] as int? ?? 0,
      fileName: json['file_name'] as String?,
      filePath: json['file_path'] as String?,
      filePreviewUrl: json['file_preview_url'] as String?,
      fileSize: json['file_size'] as int? ?? 0,
      bucket: json['bucket'] as String?,
      key: json['key'] as String?,
      host: json['host'] as String?,
      accessid: json['accessid'] as String?,
      callback: json['callback'] as String?,
      callbackVar: json['callback-var'] as String?,
      policy: json['policy'] as String?,
      signature: json['signature'] as String?,
      expire: json['expire'] as String?,
    );

jiechic avatar Jul 18 '23 03:07 jiechic