The json response looks like this:
{
"name": "A name",
"international": {
"name": {
"en": "A name",
"fr": "Un nom"
}
}
}
I tried the following code to deserialize it:
import 'package:json_annotation/json_annotation.dart';
import 'dart:convert';
part 'model.g.dart';
void main() {
var json = '''{
"name": "A name",
"international": {
"name": {
"en": "A name",
"fr": "Un nom"
}
}
}''';
var model = Model.fromJson(jsonDecode(json));
}
@JsonSerializable()
class Model {
Model({
required this.name,
required this.internationalNames,
});
factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
final String name;
@JsonKey(readValue: _readInternational, name: "name")
final Map<String, String> internationalNames;
static Object? _readInternational(Map<dynamic, dynamic> json, String key) =>
json['international'][key];
}
I am getting an error when running the build_runner:
More than one field has the JSON key for name "name"
even though the internationalNames one is nested inside the international object, so there is no duplicate keys. What could I do to deserialize this?