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
- 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:
States | Type | Example |
---|---|---|
normal | Normal message | Hey |
firstConversation | Chatroom first message | Tony Stark started this chatroom |
memberJoinedOpenChatroom | Member joins open chatroom | Tony Stark joined this chatroom |
memberLeftOpenChatroom | Member leaves open chatroom | Tony Stark left this chatroom |
memberAddedToChatroom | Member added to chatroom | Nick Fury added Tony Stark |
memberLeftSecretChatroom | Member leaves secret chatroom | Tony Stark left this chatroom |
memberRemovedFromChatroom | Member removed from chatroom | Nick Fury removed Tony Stark |
poll | Poll message | Nick Fury created a poll: "Who should lead the Avengers?" |
allMembersAdded | All members added to chatroom | Nick Fury added all members |
topicChanged | Chatroom topic changed | Nick 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.