Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jiechic opened this issue Jul 18, 2023 · 1 comment · May be fixed by #1343
Open

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

jiechic opened this issue Jul 18, 2023 · 1 comment · May be fixed by #1343

Comments

@jiechic
Copy link

jiechic commented Jul 18, 2023

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?,
    );
@ljmatan
Copy link

ljmatan commented Oct 15, 2024

For whatever reason, the current workaround is to define converters both as annotations and as list members, however, it must be done separately for nullable and non-nullable types, like so:

@JsonSerializable(
  converters: [
    ExampleSerializationHandlerString(),
    ExampleSerializationHandlerBool(),
    ExampleSerializationHandlerInt(),
    ExampleSerializationHandlerDouble(),
    ExampleSerializationHandlerMap(),
  ],
)
@ExampleSerializationHandlerStringNullable()
@ExampleSerializationHandlerBoolNullable()
@ExampleSerializationHandlerIntNullable()
@ExampleSerializationHandlerDoubleNullable()
@ExampleSerializationHandlerMapNullable()

This is with the latest json_serializable (6.8.0) and json_annotation (4.9.0) versions at the time of this writing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants