我为我的应用程序开发了一个Rest API。它以以下格式2018-09-07T17:29:12+02:00
发送到app日期,我猜+2:00代表我的时区作为一个对象的一部分。
在我的Flutter应用程序中,一旦我反序列化接收到的对象,它将两个小时减去实际接收的DateTime对象。
我正在尝试反序列化的类定义如下:
import 'package:json_annotation/json_annotation.dart';
part 'evento.g.dart';
@JsonSerializable(nullable: false)
class Evento {
final int id;
final String nombre;
final String discoteca;
final int precio_desde;
final int edad_minima;
final DateTime fecha_inicio;
final DateTime fecha_fin;
final DateTime fecha_fin_acceso;
final String cartel;
final bool incluyeCopa;
Evento(this.id, this.nombre, this.discoteca, this.precio_desde, this.edad_minima, this.fecha_inicio, this.fecha_fin, this.fecha_fin_acceso, this.cartel, this.incluyeCopa, this.num_tipos);
factory Evento.fromJson(Map<String, dynamic> json) => _$EventoFromJson(json);
Map<String, dynamic> toJson() => _$EventoToJson(this);
}
DateTime
只能代表当地时间和UTC时间。
它支持解析时区偏移,但将其规范化为UTC
print(DateTime.parse('2018-09-07T17:29:12+02:00').isUtc);
打印true
。
然后,您只能使用toLocal()
或toUtc()
在本地和UTC时间之间进行转换
尝试在反序列化时调用.toLocal()
方法。
这就是文档所说的
使用toLocal和toUtc方法获取在其他时区中指定的等效日期/时间值。