'Flutter How to append a groupedBy to a List?

I'm new to Flutter so if there is any additional context I need to add please let me know.

I have a tab in my app where I'll need to display a list. That list will be a combination of single items & groups of items.

So an example would be:

  • Item 1 -- Welcome to this app! blah blah blah
  • Item 2 -- User A and User B just liked your post!
  • Item 3 -- User A, User B, and 15 others replied to your post!

So this List will have two different items, Just a Generic Type & then a Grouped Type.

The groups will have their own types & that's how I am grouping them. In this example, it would be grouped on liking a post or group by replying to a post.

At the end of my grouping this is what I have:

// postLikes
MapEntry(PostType.postLikes: [Post{test, test, testID1, testId2, PostType.postLikes}, Post{test, test, testID1, testID2, PostType.postLikes}])

// postReplies
MapEntry(PostType.postReplies: [Post{test, test, testID1, testID2, PostType.postReplies}])

If I do something like:

var groupedPosts = <Post>[];
groupedPosts.addAll(groupedPostsByType.value);

Then they are all added but the grouping is separated.

If there is anything else that would help please let me know.



Solution 1:[1]

As far as I understood your query,

enum PostType { normal, like, reply }

class Post {
  final PostType type;
  final DateTime time;
  final String message;
  final List<User> users;

  String get formattedUser {
    if (users == null) return "";
    if (users.length > 2)
      return "${users.first} and ${users.length - 1} others";
    return users.join(" and ");
  }

  String get formattedMessage {
    switch (type) {
      case PostType.normal:
        return message;
      case PostType.like:
        return "$formattedUser liked your post.";
      case PostType.reply:
        return "$formattedUser replied to your post.";
      default:
        return "n/a"; //replace with your default message
    }
  }

  Post({this.type, this.time, this.message, this.users});
}

class User {
  final String name;
  final String otherField;

  User(this.name, [this.otherField]);

  @override
  String toString() => this.name;
}

void main() {
  var data = [
    Post(type: PostType.normal, message: "Welcome to something!"),
    Post(type: PostType.like, users: [
      User("Mr. X"),
      User("Mr. Y"),
    ]),
    Post(type: PostType.reply, users: [
      User("Mr. A"),
      User("Mr. B"),
      User("Mr. C"),
      User("Mr. D"),
    ])
  ];

  data.forEach((element) {
    print(element.formattedMessage);
  });
  
  //TODO: In order to group the data based on their type
  var messages = data.where((element) => element.type == PostType.normal);
  var likes = data.where((element) => element.type == PostType.like);
  var replies = data.where((element) => element.type == PostType.reply);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1