Boby
Boby

Reputation: 1202

How to add data to flutter stream?

Here is how i set the stream

  StreamController<List<MessageFromCloudDetail>> _messageFromCloudDetail;

here is how add data to it

ChatApiClient().fetchDetail(http.Client(),documentId).then((data){
    _messageFromCloudDetail.add(data);
});

and here is my streambuilder

StreamBuilder <List<MessageFromCloudDetail>>(
                            stream:_messageFromCloudDetail.stream,
                            builder: (context, snapshot) {
                              switch (snapshot.connectionState) {
                                case ConnectionState.none:
                                case ConnectionState.waiting:
                                  return Container(
                                    child: Center(
                                      child: CircularProgressIndicator(),
                                    ),
                                  );
                                default:
                                  return new Flexible(
                                    child: ChatBuilder(
                                        querySnapshot: snapshot.data,
                                        userFullname: userFullname,
                                        userId: userId,
                                        roomId: documentId),
                                  );
                              }
                            }),

For the first load / open, the data is showing. But, when i try to add new data like this

    ChatApiClient().inputDetail(documentId, userId, _textController.text ).then((result){
 // [{"chatDetailId":"3","roomId":"1","from":"2","messageContent":"4","createdDate":"2019-12-02 13:53:25","chatImage":null}] 
      _messageFromCloudDetail.sink.add(result);
    });

But it is changing to the new json instead of adding new data. So my question is , how can i add new data to the stream ?

Note : please don't tell me to re-load all data again.

Here the log

First load page

I/flutter (19129): AsyncSnapshot<List<MessageFromCloudDetail>>(ConnectionState.waiting, null, null)
I/flutter (19129): AsyncSnapshot<List<MessageFromCloudDetail>>(ConnectionState.active, [Instance of 'MessageFromCloudDetail'], null)

after i submit something

W/IInputConnectionWrapper(19129): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(19129): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(19129): getTextAfterCursor on inactive InputConnection
I/flutter (19129): AsyncSnapshot<List<MessageFromCloudDetail>>(ConnectionState.active, [Instance of 'MessageFromCloudDetail'], null)

My class

class MessageFromCloudDetail {
  String roomId;
  String from;
  String messageContent;
  String createdDate;
  String chatImage;

  MessageFromCloudDetail(
      {this.roomId, this.from, this.messageContent, this.createdDate, this.chatImage});

  factory MessageFromCloudDetail.fromJson(Map<String, dynamic> json) {
    return MessageFromCloudDetail(
        roomId: json['roomId'] as String,
        from: json['from'] as String,
        messageContent: json['messageContent'] as String,
        createdDate: json['createdDate'] as String,
        chatImage: json['chatImage'] as String
    );
  }
}

Upvotes: 1

Views: 5070

Answers (2)

Boby
Boby

Reputation: 1202

Thanks to @pskink . I have fix my problem, here is how i add new data

first i change how i load the data for the first time

List<MessageFromCloudDetail> listMessage = <MessageFromCloudDetail>[];

    listMessage =
            await ChatApiClient().fetchDetail(http.Client(), documentId);
        if (this.mounted) {
          _messageFromCloudDetail.sink.add(listMessage);
        }

and then

Future _handleSubmitted(String text) async {
    MessageFromCloudDetail newMessage;
    newMessage = MessageFromCloudDetail(
        messageContent: _textController.text,
        from: userId,
        createdDate: DateTime.now().toString(),
        chatImage: "");
    listMessage.add(newMessage);
    listMessage.sort((a, b) => b.createdDate.compareTo(a.createdDate));
    _messageFromCloudDetail.sink.add(listMessage);
    _textController.text = "";
  }

Upvotes: 0

Marco Galetta
Marco Galetta

Reputation: 23

I think that your're simply replacing old data with newest one. I don't know how your data structure is made but you probably need to attach new data to the oldest.

Upvotes: 1

Related Questions