From 4a094629d11ca98454869e8afa66b0e6b3b2a223 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 20 Apr 2021 09:50:09 +0200 Subject: [PATCH] make event model const --- .../stream_chat/lib/src/models/event.dart | 27 +++++----- .../stream_chat/lib/src/models/event.g.dart | 50 ++++++++----------- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/packages/stream_chat/lib/src/models/event.dart b/packages/stream_chat/lib/src/models/event.dart index a70b2b3e..65633814 100644 --- a/packages/stream_chat/lib/src/models/event.dart +++ b/packages/stream_chat/lib/src/models/event.dart @@ -10,7 +10,7 @@ part 'event.g.dart'; @JsonSerializable() class Event { /// Constructor used for json serialization - Event({ + const Event({ this.type, this.cid, this.connectionId, @@ -27,16 +27,18 @@ class Event { this.channelId, this.channelType, this.parentId, - this.extraData, - }) : isLocal = true; + this.extraData = const {}, + this.isLocal = true, + }); /// Create a new instance from a json - factory Event.fromJson(Map json) => - _$EventFromJson(Serialization.moveToExtraDataFromRoot( - json, - topLevelFields, - )) - ..isLocal = false; + factory Event.fromJson(Map json) => _$EventFromJson({ + ...Serialization.moveToExtraDataFromRoot( + json, + topLevelFields, + ), + 'is_local': false, + }); /// The type of the event /// [EventType] contains some predefined constant types @@ -89,11 +91,11 @@ class Event { /// True if the event is generated by this client @JsonKey(ignore: true) - bool isLocal; + final bool isLocal; /// Map of custom channel extraData - @JsonKey(includeIfNull: false) - final Map? extraData; + @JsonKey(defaultValue: {}) + final Map extraData; /// Known top level fields. /// Useful for [Serialization] methods. @@ -114,7 +116,6 @@ class Event { 'channel_id', 'channel_type', 'parent_id', - 'is_local', ]; /// Serialize to json diff --git a/packages/stream_chat/lib/src/models/event.g.dart b/packages/stream_chat/lib/src/models/event.g.dart index afe1f7c5..e4889978 100644 --- a/packages/stream_chat/lib/src/models/event.g.dart +++ b/packages/stream_chat/lib/src/models/event.g.dart @@ -38,39 +38,29 @@ Event _$EventFromJson(Map json) { channelId: json['channel_id'] as String?, channelType: json['channel_type'] as String?, parentId: json['parent_id'] as String?, - extraData: json['extra_data'] as Map?, + extraData: json['extra_data'] as Map? ?? {}, ); } -Map _$EventToJson(Event instance) { - final val = { - 'type': instance.type, - 'cid': instance.cid, - 'channel_id': instance.channelId, - 'channel_type': instance.channelType, - 'connection_id': instance.connectionId, - 'created_at': instance.createdAt?.toIso8601String(), - 'me': instance.me?.toJson(), - 'user': instance.user?.toJson(), - 'message': instance.message?.toJson(), - 'channel': instance.channel?.toJson(), - 'member': instance.member?.toJson(), - 'reaction': instance.reaction?.toJson(), - 'total_unread_count': instance.totalUnreadCount, - 'unread_channels': instance.unreadChannels, - 'online': instance.online, - 'parent_id': instance.parentId, - }; - - void writeNotNull(String key, dynamic value) { - if (value != null) { - val[key] = value; - } - } - - writeNotNull('extra_data', instance.extraData); - return val; -} +Map _$EventToJson(Event instance) => { + 'type': instance.type, + 'cid': instance.cid, + 'channel_id': instance.channelId, + 'channel_type': instance.channelType, + 'connection_id': instance.connectionId, + 'created_at': instance.createdAt?.toIso8601String(), + 'me': instance.me?.toJson(), + 'user': instance.user?.toJson(), + 'message': instance.message?.toJson(), + 'channel': instance.channel?.toJson(), + 'member': instance.member?.toJson(), + 'reaction': instance.reaction?.toJson(), + 'total_unread_count': instance.totalUnreadCount, + 'unread_channels': instance.unreadChannels, + 'online': instance.online, + 'parent_id': instance.parentId, + 'extra_data': instance.extraData, + }; EventChannel _$EventChannelFromJson(Map json) { return EventChannel(