make event model const

This commit is contained in:
Salvatore Giordano
2021-04-20 09:50:09 +02:00
parent 79769acef0
commit 4a094629d1
2 changed files with 34 additions and 43 deletions
+12 -11
View File
@@ -10,7 +10,7 @@ part 'event.g.dart';
@JsonSerializable() @JsonSerializable()
class Event { class Event {
/// Constructor used for json serialization /// Constructor used for json serialization
Event({ const Event({
this.type, this.type,
this.cid, this.cid,
this.connectionId, this.connectionId,
@@ -27,16 +27,18 @@ class Event {
this.channelId, this.channelId,
this.channelType, this.channelType,
this.parentId, this.parentId,
this.extraData, this.extraData = const {},
}) : isLocal = true; this.isLocal = true,
});
/// Create a new instance from a json /// Create a new instance from a json
factory Event.fromJson(Map<String, dynamic> json) => factory Event.fromJson(Map<String, dynamic> json) => _$EventFromJson({
_$EventFromJson(Serialization.moveToExtraDataFromRoot( ...Serialization.moveToExtraDataFromRoot(
json, json,
topLevelFields, topLevelFields,
)) ),
..isLocal = false; 'is_local': false,
});
/// The type of the event /// The type of the event
/// [EventType] contains some predefined constant types /// [EventType] contains some predefined constant types
@@ -89,11 +91,11 @@ class Event {
/// True if the event is generated by this client /// True if the event is generated by this client
@JsonKey(ignore: true) @JsonKey(ignore: true)
bool isLocal; final bool isLocal;
/// Map of custom channel extraData /// Map of custom channel extraData
@JsonKey(includeIfNull: false) @JsonKey(defaultValue: {})
final Map<String, dynamic>? extraData; final Map<String, dynamic> extraData;
/// Known top level fields. /// Known top level fields.
/// Useful for [Serialization] methods. /// Useful for [Serialization] methods.
@@ -114,7 +116,6 @@ class Event {
'channel_id', 'channel_id',
'channel_type', 'channel_type',
'parent_id', 'parent_id',
'is_local',
]; ];
/// Serialize to json /// Serialize to json
@@ -38,12 +38,11 @@ Event _$EventFromJson(Map<String, dynamic> json) {
channelId: json['channel_id'] as String?, channelId: json['channel_id'] as String?,
channelType: json['channel_type'] as String?, channelType: json['channel_type'] as String?,
parentId: json['parent_id'] as String?, parentId: json['parent_id'] as String?,
extraData: json['extra_data'] as Map<String, dynamic>?, extraData: json['extra_data'] as Map<String, dynamic>? ?? {},
); );
} }
Map<String, dynamic> _$EventToJson(Event instance) { Map<String, dynamic> _$EventToJson(Event instance) => <String, dynamic>{
final val = <String, dynamic>{
'type': instance.type, 'type': instance.type,
'cid': instance.cid, 'cid': instance.cid,
'channel_id': instance.channelId, 'channel_id': instance.channelId,
@@ -60,18 +59,9 @@ Map<String, dynamic> _$EventToJson(Event instance) {
'unread_channels': instance.unreadChannels, 'unread_channels': instance.unreadChannels,
'online': instance.online, 'online': instance.online,
'parent_id': instance.parentId, 'parent_id': instance.parentId,
'extra_data': instance.extraData,
}; };
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('extra_data', instance.extraData);
return val;
}
EventChannel _$EventChannelFromJson(Map<String, dynamic> json) { EventChannel _$EventChannelFromJson(Map<String, dynamic> json) {
return EventChannel( return EventChannel(
members: (json['members'] as List<dynamic>?) members: (json['members'] as List<dynamic>?)