Flutter更改不正确

Flutter更改不正确

本文介绍了TextFormField Flutter更改不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,希望从另一个窗口小部件按编程方式进行更新。为了测试这一点,我使用相同的文本对标签文本进行了更新,并按预期工作。

I have a form that I would like to be updated programatically from another widget is pressed. To test this I made the label text update with the same text and this worked as expected.

出于某种原因,即使在调试时,初始值也不会更新,但确实显示字符串是正确的值。我添加了一个控制器来处理文本,但我仍然不明白为什么初始值不能按我预期的方式工作。

For some reason the inital value is not updated even though in debugging it does show the string to be the correct value. I added a controller to handle the text but i still don't understand why the initial value was not working the way I thought it would.

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;
            }
          });


推荐答案

这是因为 initialValue 是通过 TextEditingController initState 中设置的,因此您无法更新该值,它只是初始值。

That is because the initialValue is set in the initState with theTextEditingController so you can not update that value, it is just the 'initial value'.

检查 TextFormField 的源代码:

     @override
      void initState() {
        super.initState();
        if (widget.controller == null) {
          _controller = new TextEditingController(text: widget.initialValue);
        } else {
          widget.controller.addListener(_handleControllerChanged);
        }
      }

这篇关于TextFormField Flutter更改不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:34