Skip to content

Commit 61783bd

Browse files
committed
🐛 (bug) fix
1.:bug: fix issue mention at :- #27 (comment)
1 parent 4e87808 commit 61783bd

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

lib/model/feedModel.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ class FeedModel {
7272
}
7373
if (map["likeList"] != null) {
7474
likeList = List<String>();
75-
map['likeList'].forEach((value) {
76-
likeList.add(value);
77-
});
78-
likeCount = likeList.length;
75+
final list = map['likeList'];
76+
if (list is List) {
77+
map['likeList'].forEach((value) {
78+
likeList.add(value);
79+
});
80+
likeCount = likeList.length;
81+
}
7982
} else {
8083
likeList = [];
8184
likeCount = 0;
@@ -107,4 +110,4 @@ class FeedModel {
107110
}
108111
return isValid;
109112
}
110-
}
113+
}

lib/state/notificationState.dart

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ class NotificationState extends AppState {
1515
String notificationReciverId, notificationTweetId;
1616
FeedModel notificationTweetModel;
1717
NotificationType get notificationType => _notificationType;
18-
set setNotificationType(NotificationType type){
19-
_notificationType = type;
20-
}
18+
set setNotificationType(NotificationType type) {
19+
_notificationType = type;
20+
}
21+
2122
// FcmNotificationModel notification;
2223
String notificationSenderId;
2324
dabase.Query query;
@@ -49,7 +50,7 @@ class NotificationState extends AppState {
4950
/// get [Notification list] from firebase realtime database
5051
void getDataFromDatabase(String userId) {
5152
try {
52-
loading = true;
53+
loading = true;
5354
_notificationList = [];
5455
kDatabase
5556
.child('notification')
@@ -60,13 +61,22 @@ class NotificationState extends AppState {
6061
var map = snapshot.value;
6162
if (map != null) {
6263
map.forEach((tweetKey, value) {
63-
var model = NotificationModel.fromJson(tweetKey,value["updatedAt"],snapshot.value["type"]);
64+
var model = NotificationModel.fromJson(
65+
tweetKey, value["updatedAt"], snapshot.value["type"]);
6466
_notificationList.add(model);
6567
});
66-
_notificationList.sort((x,y)=> DateTime.parse(y.updatedAt).compareTo(DateTime.parse(x.updatedAt)));
68+
_notificationList.sort((x, y) {
69+
if (x.updatedAt != null && y.updatedAt != null) {
70+
return DateTime.parse(y.updatedAt)
71+
.compareTo(DateTime.parse(x.updatedAt));
72+
} else if (x.updatedAt != null) {
73+
return 1;
74+
} else
75+
return 0;
76+
});
6777
}
6878
}
69-
loading = false;
79+
loading = false;
7080
});
7181
} catch (error) {
7282
loading = false;
@@ -94,8 +104,7 @@ class NotificationState extends AppState {
94104
if (userList.length > 0 && userList.any((x) => x.userId == userId)) {
95105
return Future.value(userList.firstWhere((x) => x.userId == userId));
96106
}
97-
var snapshot =
98-
await kDatabase.child('profile').child(userId).once();
107+
var snapshot = await kDatabase.child('profile').child(userId).once();
99108
if (snapshot.value != null) {
100109
var map = snapshot.value;
101110
user = User.fromJson(map);
@@ -106,17 +115,17 @@ class NotificationState extends AppState {
106115
return null;
107116
}
108117
}
118+
109119
/// Remove notification if related Tweet is not found or deleted
110-
void removeNotification(String userId, String tweetkey)async{
111-
kDatabase
112-
.child('notification')
113-
.child(userId)
114-
.child(tweetkey).remove();
120+
void removeNotification(String userId, String tweetkey) async {
121+
kDatabase.child('notification').child(userId).child(tweetkey).remove();
115122
}
123+
116124
/// Trigger when somneone like your tweet
117125
void _onNotificationAdded(Event event) {
118126
if (event.snapshot.value != null) {
119-
var model = NotificationModel.fromJson(event.snapshot.key, event.snapshot.value["updatedAt"],event.snapshot.value["type"]);
127+
var model = NotificationModel.fromJson(event.snapshot.key,
128+
event.snapshot.value["updatedAt"], event.snapshot.value["type"]);
120129
if (_notificationList == null) {
121130
_notificationList = List<NotificationModel>();
122131
}
@@ -130,7 +139,8 @@ class NotificationState extends AppState {
130139
/// Trigger when someone changed his like preference
131140
void _onNotificationChanged(Event event) {
132141
if (event.snapshot.value != null) {
133-
var model = NotificationModel.fromJson(event.snapshot.key, event.snapshot.value["updatedAt"],event.snapshot.value["type"]);
142+
var model = NotificationModel.fromJson(event.snapshot.key,
143+
event.snapshot.value["updatedAt"], event.snapshot.value["type"]);
134144
//update notification list
135145
_notificationList
136146
.firstWhere((x) => x.tweetKey == model.tweetKey)
@@ -143,46 +153,46 @@ class NotificationState extends AppState {
143153
/// Trigger when someone undo his like on tweet
144154
void _onNotificationRemoved(Event event) {
145155
if (event.snapshot.value != null) {
146-
var model = NotificationModel.fromJson(event.snapshot.key, event.snapshot.value["updatedAt"],event.snapshot.value["type"]);
156+
var model = NotificationModel.fromJson(event.snapshot.key,
157+
event.snapshot.value["updatedAt"], event.snapshot.value["type"]);
147158
// remove notification from list
148159
_notificationList.removeWhere((x) => x.tweetKey == model.tweetKey);
149160
notifyListeners();
150161
print("Notification Removed");
151162
}
152163
}
164+
153165
/// Configure notification services
154-
void initfirebaseService(){
166+
void initfirebaseService() {
155167
_firebaseMessaging.configure(
156168
onMessage: (Map<String, dynamic> message) async {
157169
// print("onMessage: $message");
158170
print(message['data']);
159171
notifyListeners();
160172
},
161173
onLaunch: (Map<String, dynamic> message) async {
162-
cprint("Notification ",event: "onLaunch");
174+
cprint("Notification ", event: "onLaunch");
163175
var data = message['data'];
164176
// print(message['data']);
165177
notificationSenderId = data["senderId"];
166178
notificationReciverId = data["receiverId"];
167179
notificationReciverId = data["receiverId"];
168-
if(data["type"] == "NotificationType.Mention"){
180+
if (data["type"] == "NotificationType.Mention") {
169181
setNotificationType = NotificationType.Mention;
170-
}
171-
else if(data["type"] == "NotificationType.Message"){
182+
} else if (data["type"] == "NotificationType.Message") {
172183
setNotificationType = NotificationType.Message;
173184
}
174185
notifyListeners();
175186
},
176187
onResume: (Map<String, dynamic> message) async {
177-
cprint("Notification ",event: "onResume");
188+
cprint("Notification ", event: "onResume");
178189
var data = message['data'];
179190
// print(message['data']);
180191
notificationSenderId = data["senderId"];
181192
notificationReciverId = data["receiverId"];
182-
if(data["type"] == "NotificationType.Mention"){
193+
if (data["type"] == "NotificationType.Mention") {
183194
setNotificationType = NotificationType.Mention;
184-
}
185-
else if(data["type"] == "NotificationType.Message"){
195+
} else if (data["type"] == "NotificationType.Message") {
186196
setNotificationType = NotificationType.Message;
187197
}
188198
notifyListeners();
@@ -201,5 +211,4 @@ class NotificationState extends AppState {
201211
print(token);
202212
});
203213
}
204-
205214
}

0 commit comments

Comments
 (0)