处理Textfields的动态列表

发布于 2025-01-30 11:32:55 字数 437 浏览 3 评论 0原文

如何正确处理扑朔迷离的状态有些不直觉,尤其是在服务器端交付的内容方面。

我收到来自API的n个对象的列表。我想使用ListView显示该列表,然后将我的列表映射到一堆listTile s。在每个listTile中,我想使用一个文本字段,填充具有默认值的TextField,并能够单独处理每个TextField(最终,我想为每个TextField存储一个值存储作为字段初始值)。

但是...我该怎么做?我不能只使用单个TextedingController,因为我每个Textfield需要一个。但是,当我第一次初始化小部件时,我还不知道我需要多少个小部件,因为我的API呼叫还没有返回结果。因此,我需要以某种方式将控制器动态添加到我的小部件的构建方法中,但是由于该代码每次出于任何原因而重新粉刷我的列表时,该代码都会执行该代码,

这是解决此问题的最清洁方法?

How to properly handle state in Flutter is somewhat unintuitive, especially when it comes to depending on server-side delivered content.

I receive a list of n objects from an API. I want to display that list using ListView and mapping my list to a bunch of ListTiles. In each ListTile, I would like to use a TextField, populate that TextField with a default value, as well as being able to handle each textfield individually (Ultimately, I want to store a value for each TextField in storage, and load that value from storage as the fields initial value as well).

But... How would I go about this? I can't just use a single TextEditingController, as I need one per TextField. But when I initialize my Widget for the first time, I don't yet know how many I need, given that my API call will not have returned a result yet. So I would somehow need to add the controllers dynamically in my widget's build method, which however feels extremely unclean as that code would execute every single time my list gets repainted for whatever reason...

What would be the cleanest way to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文