mercredi 20 mars 2019

Check for consecutive value in Map?

I am make random chat app. I am store user message info in Map.

I need check if message is send by same person so can display different icon to user.

So far I have:

  bool _sameUser () {
    if (index > 0 && map != null && map[index + 1] != null && map[index + 1]['fromUser’] == map[index][‘fromUser’]) {
      return true;
    } else {
      return false;
    }
  }

(index is reverse so new message is insert at index 0)

But this only work for 1 message before current message because I am call only [index + 1]. It no work if >2 message from same user.

How I can rewrite conditional so it return true if higher index message is also from same user. But higher index must be consecutive. If one index not have same user then must stop and return false.

Use case: If user 1 send many consecutive message, no display user avatar for every message. Only display one time next to last message. But if other user 2 send message then next time user 1 send message, will show avatar again.

Thanks!

Aucun commentaire:

Enregistrer un commentaire