Skip to content

Instantly share code, notes, and snippets.

@roipeker
Created April 24, 2020 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roipeker/9c7858528f360c6a6ef1abc39e4a1182 to your computer and use it in GitHub Desktop.
Save roipeker/9c7858528f360c6a6ef1abc39e4a1182 to your computer and use it in GitHub Desktop.
Dart, compare chat messages id in list to show avatar
final list = [
Message(0), Message(0), Message(0),
Message(1), Message(1),
Message(0),
Message(2), Message(2), Message(2),
Message(0),
Message(3), Message(3),
Message(1), Message(1),
Message(4),
Message(0)
];
void main() {
Message prevMessage, currMessage;
for (var i = 0; i < list.length; ++i) {
currMessage = list[i];
if (i > 0) prevMessage = list[i - 1];
prevMessage?.compareShowAvatar(currMessage);
}
currMessage?.compareShowAvatar(prevMessage);
list.forEach(print);
}
class Message {
int uid;
bool showAvatar = false;
Message(this.uid);
void compareShowAvatar(Message other) => showAvatar=other?.uid != uid;
@override
String toString() => 'Message {uid: $uid, showAvatar: $showAvatar}';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment