I have a code that makes a chat list from json data
final jsonItems = (json.decode(jsonResponse.body))['messages'].cast<Map<String, dynamic>>();
List<Messages> usersList = jsonItems.map<Messages>((json) {
return Messages.fromJson(json);
}).toList();
I use websockets to get a json array:
{
"type": 13,
"msg": {
"id": "1",
"ownerName": "name",
"createdAt": "2020-06-23T21:39:46.427Z",
"type": 1,
"body": "Message",
}
}
Here is my builder for displaying chat messages
FutureBuilder<List<Messages>>(
future: fetchJSONData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Messages> data = snapshot.data;
return _messagesListView(data);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
This my Map
enum MessageType {sent, received}
class Messages {
MessageType status;
String contactName;
String message;
String time;
Messages({ this.status, this.message, this.contactName, this.time});
factory Messages.fromJson(Map<String, dynamic> json) {
return Messages(
contactName: json['ownerName'],
message: json['body'],
time: json['createdAt'],
status: MessageType.received
);
}
}
I have already done getting data from websockets, but how can I add a new message to the list that I received from websockets, and that the message would appear on the screen