我有一个模型,它在颤振应用程序中包含多个DateTime
对象。虽然使用注释转换非可空 对象工作得非常好,但当类型为可空对象时出现错误。DateTime
@JsonKey(fromJson: _fromJson, toJson: _toJson)
DataTime?
来自的错误build_runner
是:
Error with `@JsonKey` on `mDate`. The `toJson` function `_toJson` argument type `DateTime` is not compatible with field type `DateTime?`.
package:subscriptionbutler/models/credit_card.dart:26:18
╷
26 │ late DateTime? mDate;
│ ^^^^^
╵
我的转换函数如下所示:
static DateTime _fromJson(int int) => DateTime.fromMillisecondsSinceEpoch(int, isUtc: true);
static int _toJson(DateTime time) => time.millisecondsSinceEpoch;
它们非常适用于不可为空的 DateTime对象,但我不知道如何使它与DateTime?
.