Skip to main content

How to filter state messages in a chatroom?

With the LikeMinds Chat Flutter SDK, you have full control over which state messages appear in your chatroom. Whether it's notifications for joining/leaving members, system alerts, or other updates, you can choose exactly what stays visible and what is filtered out. This guide will walk you through the simple steps to customize message visibility, ensuring your chatroom remains clean, relevant, and distraction-free.

Prerequisites

  1. LikeMinds Chat Flutter SDK: Ensure you've installed and initialized the SDK in your Flutter application. Refer to the Flutter installation guide for detailed instructions.

Steps to Filter State Messages in Chatroom

Step 1: Define the List of States to Filter

First, create a list of the state messages you'd like to exclude from the chatroom using the ConversationState enum provided by the SDK. This step ensures only relevant messages appear, keeping conversations clean and focused.

List<ConversationState> excludedConversationStates = [
ConversationState.memberJoinedOpenChatroom,
ConversationState.memberLeftOpenChatroom,
ConversationState.memberLeftSecretChatroom,
ConversationState.memberAddedToChatroom,
];

Choose the states to filter from the following table:

StatesTypeExample
normalNormal messageHey
firstConversationChatroom first messageTony Stark started this chatroom
memberJoinedOpenChatroomMember joins open chatroomTony Stark joined this chatroom
memberLeftOpenChatroomMember leaves open chatroomTony Stark left this chatroom
memberAddedToChatroomMember added to chatroomNick Fury added Tony Stark
memberLeftSecretChatroomMember leaves secret chatroomTony Stark left this chatroom
memberRemovedFromChatroomMember removed from chatroomNick Fury removed Tony Stark
pollPoll messageNick Fury created a poll: "Who should lead the Avengers?"
allMembersAddedAll members added to chatroomNick Fury added all members
topicChangedChatroom topic changedNick Fury changed current topic to "Hey"

Step 2: Initialize LMChatCore with Filtered States

Next, initialize the LMChatCore SDK and pass the filtered states list within your application's main function. This step applies your preferences globally when your chatroom loads.

In your main.dart file, use the following initialization:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:lm_chat_sdk/lm_chat_sdk.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

await LMChatCore.instance.initialize(
excludedConversationStates: excludedConversationStates,
);

runApp(const LMChatSampleApp());
}

Conclusion

By filtering state messages, you enhance the clarity and usability of your chatroom. Following these straightforward steps, you gain complete control over message visibility, ensuring a streamlined chat experience for your users.