Skip to content

Chat & Messenger

Source: pharmanet/lib/features/pharmacist/ + core/

Pages

Page File Description
ChatsPage features/pharmacist/presentation/pages/chats_page.dart Chat rooms list
ChatDetailPage features/pharmacist/presentation/pages/chat_detail_page.dart Individual chat
ChatbotPage features/pharmacist/presentation/pages/chatbot_page.dart AI chatbot
ChatSettingsPage features/pharmacist/presentation/pages/chat_settings_page.dart Chat preferences

Providers

Source: pharmanet/lib/core/providers/chat_provider.dart

Provider Type Description
chatRoomsStreamProvider StreamProvider<List<ChatRoom>> Real-time chat rooms
chatMessagesStreamProvider StreamProvider.family<String> Real-time messages per room
totalUnreadCountProvider StreamProvider<int> Global unread badge count
typingStatusProvider StreamProvider.family Typing indicator
isSendingProvider Provider<bool> Sending state

API

Source: pharmanet/lib/core/api/chat_api.dart

Method Description
streamChats() Realtime chat room stream
streamMessages(chatId) Realtime message stream
sendMessage(chatId, content, type, attachmentUrl) Send text/image message
getOrCreateChat(otherUserId, productId) Get existing or create new chat
markAsRead(chatId) Mark all as read
uploadToCloudinary(filePath, bytes) Upload image to Cloudinary
streamTypingStatus(chatId, userId) Typing indicator stream
broadcastTyping(chatId, isTyping) Broadcast typing status
deleteChat(chatId) Delete chat room
clearChatHistory(chatId) Clear messages

Real-Time Architecture

Chats use Supabase Realtime for instant message delivery:

Stream<List<Map<String, dynamic>>> streamMessages(String chatId) {
  return supabase
    .from('messages')
    .stream(primaryKey: ['id'])
    .eq('chat_id', chatId)
    .order('created_at');
}

Chat Initiation

Chats are initiated from: 1. Product detail → "Contact Seller" button creates chat with productId context 2. Pharmacy profile → "Chat" button creates chat with pharmacy 3. Order detail → "Message Seller" links to existing chat

AI Chatbot

The ChatbotPage connects to a chatbot API server:

Method Description
setBaseUrl(url) Configure chatbot server URL
checkHealth() Health check endpoint
ask(question) Send question, get AI response

Source: pharmanet/lib/core/api/chatbot_api.dart