Skip to content

Found more than one matching converter for int?. #1339

Description

@jiechic

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?,
    );

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions