如何使提供者听众只听一次

发布于 2025-01-21 19:14:03 字数 1109 浏览 3 评论 0原文

我正在使用Textfield并在列表中显示输入(文本),我正在使用Textfield的Onchanged属性和提供商来更新列表中的新元素的文本,但是,一旦该元素为元素,列表中的所有元素都会更新为Onchanged的新值添加到列表中,我希望它停止聆听Onchanged的更改。因此,我可以显示具有不同元素的列表。我如何实现。

TextField(
                    autofocus: true,
                    decoration: kTextFieldDecocation.copyWith(
                        hintText: 'B Name'),
                    onChanged: (newbName) {
                      Provider.of<BNameControllerClass>(context,
                              listen: false)
                          .newBorrowerName(newbName);
                    },
                  ),

列表元素的文本

Text(
                          Provider.of<BNameControllerClass>(context,
                                  listen: true)
                              .bName,

                          style: const TextStyle(fontSize: 20),
                        );

提供商类

class BNameControllerClass extends ChangeNotifier {
  String bName = 'Ganesh';
  newBorrowerName(String newName) {
    bName = newName;

    notifyListeners();
  }
}

I am geting input(text) using textfield and displaying in a list, I am using onchanged property of textfield and provider to update the text of new element in list but, all elements in the list update to onChanged's new value, once the element is added to list I want it to stop listening to changes of onChanged. So, that I can display list with different elements. How do I achieve that.

TextField(
                    autofocus: true,
                    decoration: kTextFieldDecocation.copyWith(
                        hintText: 'B Name'),
                    onChanged: (newbName) {
                      Provider.of<BNameControllerClass>(context,
                              listen: false)
                          .newBorrowerName(newbName);
                    },
                  ),

List element's text

Text(
                          Provider.of<BNameControllerClass>(context,
                                  listen: true)
                              .bName,

                          style: const TextStyle(fontSize: 20),
                        );

provider class

class BNameControllerClass extends ChangeNotifier {
  String bName = 'Ganesh';
  newBorrowerName(String newName) {
    bName = newName;

    notifyListeners();
  }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2025-01-28 19:14:03

创建list&lt; string&gt; textsfromInput = [];
listView.generate()生成文本小部件。
然后,在Textfield中,您可以使用onsubmittit:(value)=&gt; textsfrominput.add(value)
根据您拥有的状态管理,您可以调用SetState()或使用Bloc Builder build当build wher:pronos.textsfrominput.length!= current.current.textsfrominput.length.length.length.length.length.length或提供提供商。

Create List<String> textsFromInput = [];
Generate text widgets with ListView.generate().
Then in TextField you can use onSubmitted: (value) => textsFromInput.add(value)
Depending on what state management you have you can then call setState() or handle list rebuilding with bloc builder buildWhen: previous.textsFromInput.length != current.textsFromInput.length or with provider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文