英文:
Flutter doesn't delete last item in list
问题
我有一个包含小部件(messages
我已经尝试过
messages.remove(Widget);
messages.removeAt(index);
我甚至尝试过 messages.clear()
|
但它也不起作用...我可以删除除最后一个之外的所有消息...我还尝试将列更改为ListView.builder...请给我一个建议..
我的页面代码:
....
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,
),
),
],
),
);
}
),
....
英文:
I have a list which contains widgets (messages<Widget>) , 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,
),
),
],
));
}
),
....
答案1
得分: 0
你可以使用专用命令来移除最后一个元素,假设你有类似于 List<Widget> messages
这样的东西。命令是 messages.removeLast()
。如果这不能解决你的问题,请提供更多细节,比如你的变量 messages
的类型。
英文:
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
.
答案2
得分: 0
我将为您翻译以下代码部分:
我将StreamBuilder更改为常规的监听器,并似乎正在工作
_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");
}
}
});
和小部件
Expanded(
child: ListView.builder(
controller: scrollController,
itemCount: chat.length,
itemBuilder: (context, index) {
return generateMessage(chat[index]);
})),
请注意,我已将HTML实体字符(例如"
和&
)还原为原始文本。
英文:
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]);
})),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论