skip to Main Content

I have a list which contains widgets (messages) , the problem is that i can’t delete last item from list.
I already tried

messages.remove(Widget);
messages.removeAt(index);

I even tried messages.clear()|

But it also doesn’t work…. I can delete all message except last one… I also tried change column to listview.builder… Please give me an advice..

my page code:

    ....
    StreamBuilder(
                            stream: _channel.stream,
                            builder:
                                (context, AsyncSnapshot<dynamic> snapshot) {
                              if (snapshot.hasData) {
                                RocketChatMessage? mes;
                                if (snapshot.hasData) {
                                  try {
                                    var msg = RocketChatMessage.fromJson(
                                        json.decode(snapshot.data));
                                    if (msg.fields != null) {
                                      if (msg.fields!.args![0].msg != null &&
                                          (msg.fields!.args![0].msg!
                                                  .isNotEmpty ||
                                              msg.fields!.args![0].file !=
                                                  null)) {
                                        if (chat.isEmpty) {
                                          chat.add(msg);
                                          messages.add(generateMessage(msg));
                                        } else {
                                          var item = msg.fields!.args![0].sId;
                                          var ex =
                                              chat.last.fields!.args![0].sId;
                                          if (item != ex) {
                                            chat.add(msg);
                                            messages.add(generateMessage(msg));
                                            scrollDown();
                                          }
                                        }
                                      }
                                    }
                                  } catch (e) {
                                    getChatStory(false);
                                    print("story");
                                  }
                                }
                              }
                              
                              return Expanded(
                                  child: Stack(
                                children: [
                                  SingleChildScrollView(
                                    controller: scrollController,
                                    child: Column(
                                      children: messages,
                                    ),
                                  ),
                                ],
                              ));
                            } 
                            ),
....

2

Answers


  1. Chosen as BEST ANSWER

    i changed StreamBuilder , to usual listener , and seems like working

    _channel.stream.listen((event) {
        RocketChatMessage? mes;
        if (event != null) {
          try {
            var msg = RocketChatMessage.fromJson(json.decode(event));
            if (msg.msg == "changed" && msg.fields != null) {
              if (msg.fields!.args![0].msg != null &&
                  (msg.fields!.args![0].msg!.isNotEmpty ||
                      msg.fields!.args![0].file != null)) {
                if (chat.isEmpty) {
                  chat.add(msg);
                  messages.add(generateMessage(msg));
                } else {
                  var item = msg.fields!.args![0].sId;
                  var ex = chat.last.fields!.args![0].sId;
                  if (item != ex) {
                    chat.add(msg);
                    messages.add(generateMessage(msg));
                  }
                }
                setState(() {});
                scrollDown();
              }
            }
          } catch (e) {
            getChatStory(false);
            print("story");
          }
        }
      });
    

    and widget

     Expanded(
                            child: ListView.builder(
                                controller: scrollController,
                                itemCount: chat.length,
                                itemBuilder: (context, index) {
                                  return generateMessage(chat[index]);
                                })),
    

  2. Assuming that you have something like List<Widget> messages, you can use the dedicated command to remove the last element. It will be messages.removeLast(). If this does not answer your questions, please provide more details like the type of your variable messages.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search