0

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

1 Answer 1

1

You need to use ConnectionState inside your builder. Look at this code template: (Currently your builder return Text Widget without waiting for the future to complete)

return FutureBuilder<List<Messages>>(
     future: fetchJSONData(),
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        // future complete
        // if error or data is false return error widget
        if (snapshot.hasError || !snapshot.hasData) {
          return Text("${snapshot.error}");
        }

        // return data widget
         List<Messages> data = snapshot.data;
         return _messagesListView(data);

        // return loading widget while connection state is active
      } else
        return Center(
        child: CircularProgressIndicator());
             
    },
  );
Sign up to request clarification or add additional context in comments.

6 Comments

Until I do something on the screen, the message doesn't appear
what do you mean by do something on the screen?
Let's say that until I click anywhere in the app, the message doesn't appear
Same. Maybe need use StreamBuilder? I'm beginner in flutter
StreamBuilder used for changes in the displayed data. In your case, FutureBuilder is fine. Did you copy my code snippet and the Text widget is not showing?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.