flutter - 如果第一个为空,则将其他键的值存储在飞镖模型中 - Flutter
问题描述
我正在使用该json_annotation
包来构建我的飞镖模型。
我有一种情况,我需要将 API 响应中的任一值存储在我的name
密钥中。
API 正在返回两个密钥alias
和name
. 在我的模型中,我需要以这样的方式存储值,如果alias
(非空)中有值,则name
(模型中)将有一个alias
值。否则,name
(来自 API)密钥将存储在模型的name
密钥中。
代码 -
import 'package:json_annotation/json_annotation.dart';
part 'nw_wealth.g.dart';
@JsonSerializable(anyMap: true)
class NwWealth {
final int accountId;
final String name;
final double current;
final double invested;
final double gain;
final String type;
final double gainPercent;
final DateTime lastUpdated;
NwWealth(
this.name, // this.name should be = alias ?? name;
this.current,
this.invested,
this.gain,
this.type,
this.gainPercent,
this.accountId,
this.lastUpdated,
);
factory NwWealth.fromJson(Map<String, dynamic> json) =>
_$NwWealthFromJson(json);
Map<String, dynamic> toJson() => _$NwWealthToJson(this);
}
有没有办法,我可以根据可用性将实际名称和别名存储到模型中的同一个键中?
我确实检查了JsonKey()
对象。但找不到解决办法。
解决方案
推荐阅读
- sql - 使用 Oracle PL/SQL 定位历史记录
- javascript - 无法访问 javascript 数组,并且循环不起作用
- javascript - 登录vuejs后如何更新导航栏链接
- c# - 在 Rider for c# 中使用 Chart.SaveImage()
- javascript - 从 Javascript 将数组传递给 PHP
- java - 未能找到包含 /storage/771F-7614/Android/data/com.example.x/files/x/Photos/x_20200417044051.jpg 的已配置根目录
- powershell - Powershell - 将值传递给参数
- sql - 使用 SQL 获取唯一 ID 但不会丢失任何数据
- javascript - 在 HTML 数据属性中将 PHP 数组编码为 JSON 表示
- excel - 自动过滤工作表列表并删除标记的工作表并从示例工作表创建新的更新工作表