Created
April 24, 2020 22:45
-
-
Save roipeker/9c7858528f360c6a6ef1abc39e4a1182 to your computer and use it in GitHub Desktop.
Dart, compare chat messages id in list to show avatar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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