我有一个表单,我想通过另一个小部件以编程方式进行更新。为了测试这一点,我使用相同的文本更新了标签文本,这按预期工作。
由于某种原因,即使在调试中它确实显示字符串是正确的值,初始值也不会更新。我添加了一个控制器来处理文本,但我仍然不明白为什么初始值没有像我想象的那样工作。
new TextFormField(
key: _orderAddEditOrderFormKey,
decoration: InputDecoration(
labelText: order.order == null ? 'Order' : order.order),
initialValue: order.order == null ? "" : order.order,
),
setState(() {
isBuildOrderUsed = true;
if (this.order == null) {
this.order = new Order(order: order);
} else {
this.order.order = order;
}
});
Run Code Online (Sandbox Code Playgroud)
那是因为initialValue设置在initStatewith 中,TextEditingController所以您不能更新该值,它只是“初始值”。
检查源代码TextFormField:
@override
void initState() {
super.initState();
if (widget.controller == null) {
_controller = new TextEditingController(text: widget.initialValue);
} else {
widget.controller.addListener(_handleControllerChanged);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3140 次 |
| 最近记录: |