gpt4 book ai didi

flutter - Flutter TextField与 Controller 根本不更新该字段

转载 作者:行者123 更新时间:2023-12-03 03:24:47 25 4
gpt4 key购买 nike

我以为我拥有这项权利,但显然有些问题了。简单文本字段,默认文本由 Controller 设置为newUser['firstName']newUser的类型为Map<String, Dynamic>

TextField(
decoration: InputDecoration(labelText: 'First Name'),
controller: TextEditingController(text: newUser['firstName']), // currently set to "Garrett"
onChanged: (text) {
print('TEXT: $text'); // only gives the current value plus the last letter hit, ex.
"Garrettr", "Garrettf", etc.

setState(() {
newUser['firstName'] = text;
});

print('FIRST NAME: ${newUser['firstName']}'); // same as text
},
),

没有错误,但是当我在字段中键入更改文本时,什么也没有发生,并且 newUser['firstName']将等于“Garrette”或“Garrettr”等。如您在代码中看到的那样,通过 text传入的 onChanged似乎并不是的,虽然不确定我在做什么错。

最佳答案

在TextField初始化类似final TextEditingController anyThing = new TextEditingController();之类的代码之前,先检查一下代码

现在获取新文本集newUser['firstName'] = anyThing.text
试试这个,让我知道您是否需要进一步的帮助。

因此,整个过程将是:

final TextEditingController anyThing = new TextEditingController();
TextField(
decoration: InputDecoration(labelText: 'First Name'),
controller: anyThing, // currently set to "Garrett"
onChanged: (text) {
print('TEXT: $text'); // only gives the current value plus the last letter hit, ex.

setState(() {
newUser['firstName'] = text;
});

print('FIRST NAME: ${newUser['firstName']}'); // same as text
},
),

关于flutter - Flutter TextField与 Controller 根本不更新该字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58974093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com