How to perform Post Creation without using LikeMinds UI?
Introduction
This guide will walk you through the process of implementing a custom post creation flow using the LikeMinds Feed Flutter SDK. You'll learn how to use the LMFeedPostBloc
to create posts and handle attachments using the LMAttachmentViewData
class.
Prerequisites
Before you begin, ensure the following:
- LikeMinds Feed Flutter SDK: The SDK must be properly installed and initialized in your Flutter project. Refer to the installation guide if needed.
- Feed Enabled: Ensure that Feed is enabled on the dashboard for your project.
- Basic Understanding of Flutter Widgets: Familiarity with Flutter widgets and layout concepts.
- Knowledge of Builder Pattern: Understanding of the builder pattern in Dart, as it is used to customize and create widgets dynamically.
Steps
Step 1: Understanding the LMFeedPostBloc
The LMFeedPostBloc
is responsible for managing the state of post-related operations in the Feed SDK. It gives access to a number of different events and states that can be used to build your entire Post handling UI without worrying about the business logic.
To create a post, you'll need to add an event to the BLoC called LMFeedCreateNewPostEvent
, which we will discuss further.
Step 2: Understanding LMAttachmentViewData
The LMAttachmentViewData
class represents media attachments for a post. It's has multiple factory constructors for different scenarios.
Here's a brief overview:
enum LMMediaType { none, video, image, document, link, widget, repost, poll }
class LMAttachmentViewData {
final LMMediaType attachmentType;
final LMAttachmentMetaViewData attachmentMeta;
// Factory constructors
factory LMAttachmentViewData.fromAttachmentMeta({...});
factory LMAttachmentViewData.fromMediaUrl({...});
factory LMAttachmentViewData.fromMediaBytes({...});
factory LMAttachmentViewData.fromMediaPath({...});
// Method to map media type to integer
int mapMediaTypeToInt() {...}
}
The class uses different factory constructors based on how you want to create the attachment:
fromAttachmentMeta
: When you already have anLMAttachmentMetaViewData
object.fromMediaUrl
: When you have a URL for the media.fromMediaBytes
: When you have the media as bytes (Uint8List
).fromMediaPath
: When you have a local file path for the media.
The LMMediaType
enum provides various media types you can use.
When creating an attachment, make sure to specify the correct media type. To do this you can also use, but always prefer using the LMMediaType
enum.
LMAttachmentViewData attachment; //image type attachment
/// Function to map the LMMediaType enum to its int value
int attachmentType = attachment.mapMediaTypeToInt(); // 1
/// Function to map the attachment int value to its LMMediaType enum
LMMediaType type = mapIntToMediaType(attachmentType); // LMMediaType.image
Step 3: Creating Attachments
To create an attachment for your post, use one of the factory constructors of LMAttachmentViewData
. Here's an example using fromMediaPath
:
LMAttachmentViewData attachment = LMAttachmentViewData.fromMediaPath(
path: "path/to/your/file",
attachmentType: LMMediaType.image,
// Add other parameters as needed
);
Step 4: Preparing Post Data
Before creating a post, you need to prepare the necessary data. Here's a breakdown of the required information:
LMUserViewData? user
: The current user object (optional).String? postText
: The main content of the post (optional).String? heading
: The post heading (optional).int? feedroomId
: The ID of the feedroom where the post will be created (if applicable).List<LMUserTagViewData>? userTagged
: Any users tagged in the post (optional).List<LMTopicViewData> selectedTopics
: Any topics associated with the post.List<LMAttachmentViewData>? postMedia
: An array ofLMAttachmentViewData
objects representing attachments (optional).
- Either one of
postText
,heading
, orpostMedia
should be not null. - The parameter
selectedTopics
can be an empty list, but should not be null. - You can get the instance of the current logged-in user anytime using the helper function of the local preference utility -
LMFeedLocalPreference.instance.fetchUserData();
.
Step 5: Creating a Post
To create a post, use the following code snippet:
LMFeedPostBloc.instance.add(
LMFeedCreateNewPostEvent(
user: user, // Current user object
postText: postContent, // The main content of the post
selectedTopics: selectedTopics, // List of selected topics
postMedia: [attachment], // List of LMAttachmentViewData objects
heading: postHeading, // Optional heading
feedroomId: feedroomId, // ID of the feedroom (if applicable)
userTagged: taggedUsers, // List of tagged users
),
);
Step 6: Listening for Post Creation Result
The LMFeedPostBloc
automatically handles all the state changes, and the built-in UI responds to it accordingly. Additionally, to listen to the result of the post creation:
Listen to the LMFeedPostBloc
state changes:
LMFeedPostBloc.instance.stream.listen((state) {
if (state is LMFeedPostCreatedState) {
// Post created successfully
// Handle success (e.g., show a success message, navigate to feed)
} else if (state is LMFeedPostErrorState) {
// Error occurred while creating post
// Handle error (e.g., show error message)
}
});
Steps to Implement Post Creation with Custom Widget
Step 1: Build an Attachment
Using the steps above as reference, now build an attachment object of type LMAttachmentViewData
and add it into your list of attachments. Here's how:
List<LMAttachmentViewData> attachments = [];
final attachment = LMAttachmentViewData.fromAttachmentMeta(
attachmentType: LMMediaType.widget,
attachmentMeta: (LMAttachmentMetaViewDataBuilder()
..meta({
// Map<String, dynamic>
}))
.build(),
);
attachments.add(attachment);
Step 2: Create Post with Attachment
Use your list of attachments to send alongside your post data, in the LMFeedCreateNewPostEvent
event, and then add the event to an instance of LMFeedPostBloc
.
Additional Information
- The
LMAttachmentViewData
class uses a builder pattern. If you need more control over the creation process, you can useLMAttachmentViewDataBuilder
. - Handle attachment uploads separately before creating the post, and use the returned URLs or file paths to create
LMAttachmentViewData
objects. - Be mindful of the different media types and use the appropriate factory constructor for each type.
- Implement proper error handling and user feedback in your UI.
- For polls, widgets, or other complex media types, refer to the SDK documentation for specific parameters and usage.
- For more details on post creation features or specific media types, please reach out to us.