Skip to content

Commit d24a231

Browse files
committed
refactor: Push Notification helper class make all fields optional and migrate dynamics to Object?
1 parent c87e2ee commit d24a231

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

lib/src/utils/push_notification.dart

+45-38
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import 'dart:convert';
22

33
/// Push Notification object from https://spec.matrix.org/v1.2/push-gateway-api/
44
class PushNotification {
5-
final Map<String, dynamic>? content;
5+
final Map<String, Object?>? content;
66
final PushNotificationCounts? counts;
7-
final List<PushNotificationDevice> devices;
7+
final List<PushNotificationDevice>? devices;
88
final String? eventId;
99
final String? prio;
1010
final String? roomAlias;
@@ -17,7 +17,7 @@ class PushNotification {
1717
const PushNotification({
1818
this.content,
1919
this.counts,
20-
required this.devices,
20+
this.devices,
2121
this.eventId,
2222
this.prio,
2323
this.roomAlias,
@@ -30,39 +30,44 @@ class PushNotification {
3030

3131
/// Generate a Push Notification object from JSON. It also supports a
3232
/// `map<String, String>` which usually comes from Firebase Cloud Messaging.
33-
factory PushNotification.fromJson(Map<String, dynamic> json) =>
33+
factory PushNotification.fromJson(Map<String, Object?> json) =>
3434
PushNotification(
3535
content: json['content'] is Map
36-
? Map<String, dynamic>.from(json['content'])
36+
? Map<String, Object?>.from(json['content'] as Map)
3737
: json['content'] is String
38-
? jsonDecode(json['content'])
38+
? jsonDecode(json['content'] as String)
3939
: null,
4040
counts: json['counts'] is Map
41-
? PushNotificationCounts.fromJson(json['counts'])
41+
? PushNotificationCounts.fromJson(
42+
json['counts'] as Map<String, Object?>,
43+
)
4244
: json['counts'] is String
43-
? PushNotificationCounts.fromJson(jsonDecode(json['counts']))
45+
? PushNotificationCounts.fromJson(
46+
jsonDecode(json['counts'] as String),
47+
)
4448
: null,
4549
devices: json['devices'] is List
4650
? (json['devices'] as List)
4751
.map((d) => PushNotificationDevice.fromJson(d))
4852
.toList()
49-
: (jsonDecode(json['devices']) as List)
53+
: (jsonDecode(json['devices'] as String) as List)
5054
.map((d) => PushNotificationDevice.fromJson(d))
5155
.toList(),
52-
eventId: json['event_id'],
53-
prio: json['prio'],
54-
roomAlias: json['room_alias'],
55-
roomId: json['room_id'],
56-
roomName: json['room_name'],
57-
sender: json['sender'],
58-
senderDisplayName: json['sender_display_name'],
59-
type: json['type'],
56+
eventId: json['event_id'] as String?,
57+
prio: json['prio'] as String?,
58+
roomAlias: json['room_alias'] as String?,
59+
roomId: json['room_id'] as String?,
60+
roomName: json['room_name'] as String?,
61+
sender: json['sender'] as String?,
62+
senderDisplayName: json['sender_display_name'] as String?,
63+
type: json['type'] as String?,
6064
);
6165

62-
Map<String, dynamic> toJson() => {
66+
Map<String, Object?> toJson() => {
6367
if (content != null) 'content': content,
6468
if (counts != null) 'counts': counts?.toJson(),
65-
'devices': devices.map((i) => i.toJson()).toList(),
69+
if (devices != null)
70+
'devices': devices?.map((i) => i.toJson()).toList(),
6671
if (eventId != null) 'event_id': eventId,
6772
if (prio != null) 'prio': prio,
6873
if (roomAlias != null) 'room_alias': roomAlias,
@@ -83,45 +88,47 @@ class PushNotificationCounts {
8388
this.unread,
8489
});
8590

86-
factory PushNotificationCounts.fromJson(Map<String, dynamic> json) =>
91+
factory PushNotificationCounts.fromJson(Map<String, Object?> json) =>
8792
PushNotificationCounts(
88-
missedCalls: json['missed_calls'],
89-
unread: json['unread'],
93+
missedCalls: json['missed_calls'] as int?,
94+
unread: json['unread'] as int?,
9095
);
9196

92-
Map<String, dynamic> toJson() => {
97+
Map<String, Object?> toJson() => {
9398
if (missedCalls != null) 'missed_calls': missedCalls,
9499
if (unread != null) 'unread': unread,
95100
};
96101
}
97102

98103
class PushNotificationDevice {
99-
final String appId;
100-
final Map<String, dynamic>? data;
101-
final String pushkey;
104+
final String? appId;
105+
final Map<String, Object?>? data;
106+
final String? pushkey;
102107
final int? pushkeyTs;
103108
final Tweaks? tweaks;
104109

105110
const PushNotificationDevice({
106-
required this.appId,
111+
this.appId,
107112
this.data,
108-
required this.pushkey,
113+
this.pushkey,
109114
this.pushkeyTs,
110115
this.tweaks,
111116
});
112117

113-
factory PushNotificationDevice.fromJson(Map<String, dynamic> json) =>
118+
factory PushNotificationDevice.fromJson(Map<String, Object?> json) =>
114119
PushNotificationDevice(
115-
appId: json['app_id'],
120+
appId: json['app_id'] as String?,
116121
data: json['data'] == null
117122
? null
118-
: Map<String, dynamic>.from(json['data']),
119-
pushkey: json['pushkey'],
120-
pushkeyTs: json['pushkey_ts'],
121-
tweaks: json['tweaks'] == null ? null : Tweaks.fromJson(json['tweaks']),
123+
: Map<String, Object?>.from(json['data'] as Map),
124+
pushkey: json['pushkey'] as String?,
125+
pushkeyTs: json['pushkey_ts'] as int?,
126+
tweaks: json['tweaks'] == null
127+
? null
128+
: Tweaks.fromJson(json['tweaks'] as Map<String, Object?>),
122129
);
123130

124-
Map<String, dynamic> toJson() => {
131+
Map<String, Object?> toJson() => {
125132
'app_id': appId,
126133
if (data != null) 'data': data,
127134
'pushkey': pushkey,
@@ -137,11 +144,11 @@ class Tweaks {
137144
this.sound,
138145
});
139146

140-
factory Tweaks.fromJson(Map<String, dynamic> json) => Tweaks(
141-
sound: json['sound'],
147+
factory Tweaks.fromJson(Map<String, Object?> json) => Tweaks(
148+
sound: json['sound'] as String?,
142149
);
143150

144-
Map<String, dynamic> toJson() => {
151+
Map<String, Object?> toJson() => {
145152
if (sound != null) 'sound': sound,
146153
};
147154
}

0 commit comments

Comments
 (0)