Flutter不会删除列表中的最后一项。

huangapple go评论68阅读模式
英文:

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&lt;dynamic&gt; 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 &amp;&amp;
                                          (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(&quot;story&quot;);
                                  }
                                }
                              }
                              
                              return Expanded(
                                  child: Stack(
                                children: [
                                  SingleChildScrollView(
                                    controller: scrollController,
                                    child: Column(
                                      children: messages,
                                    ),
                                  ),
                                ],
                              ));
                            } 
                            ),
....

答案1

得分: 0

你可以使用专用命令来移除最后一个元素,假设你有类似于 List&lt;Widget&gt; messages 这样的东西。命令是 messages.removeLast()。如果这不能解决你的问题,请提供更多细节,比如你的变量 messages 的类型。

英文:

Assuming that you have something like List&lt;Widget&gt; 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实体字符(例如&quot;&amp;)还原为原始文本。

英文:

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 == &quot;changed&quot; &amp;&amp; msg.fields != null) {
          if (msg.fields!.args![0].msg != null &amp;&amp;
              (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(&quot;story&quot;);
      }
    }
  });

and widget

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

huangapple
  • 本文由 发表于 2023年5月28日 19:14:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定