-
Notifications
You must be signed in to change notification settings - Fork 171
Description
While working on the chat functionality, we encountered an error in the GetMessagesAsync method when deserializing messages from Firebase. The error message is:
Error deserializing messages: Error converting value "[{"UserIcon":"","UserName":"","LastMessage":"","TimeAgo":"","UnreadCount":0,"Text":"ok","IsSentByUser":false}]" to type 'System.Collections.Generic.List1[socialmedia.models.MessageItem]'. Path '', line 1, position 136.
`
This error suggests that the JSON data retrieved from Firebase is not being correctly converted to a list of MessageItem objects.
Additionally, while messages are being sent and saved to Firebase correctly, they do not appear in the chat and message pages after logging out and logging back in.
GetMessagesAsync Method
public async Task<List<MessageItem>> GetMessagesAsync(string senderId, string recipientId) { var messages = new List<MessageItem>(); var json = await _firebaseService.GetDataAsync($"chats/{senderId}/{recipientId}"); if (!string.IsNullOrEmpty(json) && json != "null") { try { messages = JsonConvert.DeserializeObject<List<MessageItem>>(json); } catch (JsonSerializationException ex) { Debug.WriteLine($"Error deserializing messages: {ex.Message}"); // Handle the error, e.g., log it or return an empty list messages = new List<MessageItem>(); } } return messages; }
SaveMessageAsync Method
`public async Task SaveMessageAsync(string senderId, string recipientId, MessageItem message)
{
var messages = await GetMessagesAsync(senderId, recipientId);
messages.Add(message);
var updatedJson = JsonConvert.SerializeObject(messages, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
await _firebaseService.SetDataAsync($"chats/{senderId}/{recipientId}", updatedJson);
}
OnSendButtonClicked Method in ChatPage.xaml.csprivate async void OnSendButtonClicked(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(NewMessage))
{
var chatMessage = new ChatMessage
{
Text = NewMessage,
BackgroundColor = "#4CAF50"
};
Messages.Add(chatMessage);
NewMessage = string.Empty;
OnPropertyChanged(nameof(NewMessage));
// Save the message to the database for both users
var messageItem = new MessageItem
{
Text = chatMessage.Text,
IsSentByUser = true,
UserName = ChatUserName
};
await _userService.SaveMessageAsync(_loggedInUserId, ChatUserName, messageItem);
await _userService.SaveMessageAsync(ChatUserName, _loggedInUserId, messageItem);
}
}
MessageItemsusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace socialmedia.models
{
public class MessageItem
{
public string UserIcon { get; set; }
public string UserName { get; set; }
public string LastMessage { get; set; }
public string TimeAgo { get; set; }
public int UnreadCount { get; set; }
public string Text { get; set; }
public bool IsSentByUser { get; set; }
public MessageItem()
{
UserIcon = string.Empty;
UserName = string.Empty;
LastMessage = string.Empty;
TimeAgo = string.Empty;
Text = string.Empty;
}
}
}
`