如何使提供者听众只听一次
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建
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.