是否有一种方法可以将字段转换为蛇形,以用于映射dart中来自rest调用的响应?
我的意思是我想在一个地方声明映射发生在 snake_case 中,而不必为我创建的每个模型都提及?
正在寻找与我们如何通过 Java 中的 ObjectMapper 和 PropertyNamingStrategy.SNAKE_CASE 来实现此目标类似的东西。
在颤抖的情况下进行休止呼叫(紧跟蛇形外壳),并且仅当我以蛇形格式命名字段时才能映射。
尝试了以下步骤,它们没有任何区别,但最终得到空值。
class FeatureResponse {
@JsonValue("trending_posters") // tried this from json_annotation package
List<Poster> trendingPosters;
FeatureResponse({this.trendingPosters});
factory FeatureResponse.fromJson(Map<String, dynamic> data) => _$FeatureResponseFromJson(data);
Map<String, dynamic> toJson() => _$FeatureResponseToJson(this);
}
或者,有一个软件包 dart_json_mapper 可以帮助进行蛇案映射。
但这与 json_annotation 包冲突,后者用于自动为上述对象生成零件文件。
有没有解决的办法?优选地,不需要额外的包装。请指教。
作为参考,下面的方法会起作用,但不希望在蛇形案例中命名字段以支持这一点。
class FeatureResponse {
List<Poster> trending_posters;
FeatureResponse({this.trending_posters});
factory FeatureResponse.fromJson(Map<String, dynamic> data) => _$FeatureResponseFromJson(data);
Map<String, dynamic> toJson() => _$FeatureResponseToJson(this);
}