【发布时间】:2021-10-11 05:27:37
【问题描述】:
我在 Flutter 中的 fromJson() 方法出现错误:
factory ReminderModel.fromJson(Map<String, dynamic> json) => ReminderModel(
id: json["id"] == null ? null : json["id"],
dayName: json["dayName"] == null ? null : json["dayName"],
workStartTime: DateTime.parse(json["workStartTime"]),
workEndTime: DateTime.parse(
json["workEndTime"],
),
singleReminderModel: json["singleReminderModel"] == null
? null
: List<SingleReminderModel>.from(
json["singleReminderModel"].map(
(x) => x.toString(),
),
),
frequency: json["frequency"] == null ? null : json["frequency"],
breakPeriod: json["isPending"] == null ? null : json["breakPeriod"],
);
我的模型如下所示:
int? id;
String? dayName;
DateTime? workStartTime;
DateTime? workEndTime;
List<SingleReminderModel>? singleReminderModel;
Duration? frequency;
Duration? breakPeriod;
ReminderModel({
this.id,
this.dayName,
this.workStartTime,
this.workEndTime,
this.singleReminderModel,
this.frequency,
this.breakPeriod,
});
我需要准确地将duration 保存到string 并返回到duration。
当我将这样的数据保存在我的 toJson() 方法中时,我没有收到任何错误
"frequency": frequency!.toString(),
**更新:** 在建议的编辑之后,我收到了这个错误:
【问题讨论】:
-
你需要投到
Duration,可以加到Json吗? -
如何添加演员表?你能举个例子吗?是的,我可以添加到Json
-
那么请添加,以便我确定答案。
json["frequency"] as Duration(). -
完成请检查