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
i changed StreamBuilder , to usual listener , and seems like working
and widget
Assuming that you have something like
List<Widget> messages
, you can use the dedicated command to remove the last element. It will bemessages.removeLast()
. If this does not answer your questions, please provide more details like the type of your variablemessages
.