【问题标题】:setState not working properly with substring function insidesetState 不能与内部的子字符串函数一起正常工作
【发布时间】:2020-06-24 07:57:30
【问题描述】:

我正在尝试从子字符串函数中设置状态,但每次都没有调用 setState,这是我的代码:

...
onChanged: (e) {
                print(e);
                setState(() {
                  _code = e.substring(0, 6); // The problem!
                });
                if (e.length >= 6) _focusNode.unfocus();
              },
...

【问题讨论】:

  • 你能提供更多关于你的上下文的描述吗?正在改变什么?你为什么使用子字符串?如果您提供更多代码,最好了解。
  • @billion onChanged 是每次 TextFormField 的值发生变化时调用的 TextFormField 方法,所以在这里每当我的 TextFormField 值发生变化时,我需要将 _code 设置为前 6 个e值的位数

标签: android flutter setstate


【解决方案1】:

您的代码不清楚。 'e'上有什么 ??

尝试:

let e = toString(e);
e.substring(0,6);

【讨论】:

  • e 这是 TextFormField 发生变化时的值。我需要将 _code 设置为值 e 的前 6 位
  • 你能把'e'的控制台发给我吗??
  • 它不会控制任何东西 setState 中的代码没有完全执行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多