一点点的价值给我无效

发布于 2025-02-04 04:13:15 字数 873 浏览 3 评论 0原文

当我单击“升高”按钮时,我正在打印无效。

如果用户不输入任何内容,我想查看初始值。 如果对初始值进行修改,则打印语句应打印修改后的语句(其中)。

代码:

class _HomeState extends State<Home> {
  var _input;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('hello')),
      body: Column(
        children: [
          TextFormField(
            initialValue: 'initial value ',
            onChanged: (text) {
              setState(() {
                _input = text;
              });
            },
          ),
          RaisedButton(
            onPressed: () {
              print(_input);
            },
            child: Text('Press me'),
          )
        ],
      ),
    );
  }
}

如果我不编辑TextFormField输出,

flutter: null

则输出如果我编辑TextFormField

flutter: initial value test

I am printing null when I am clicking the raised button.

I want to see the initialValue if the user doesnot enter any thing.
If the modifies the initialValue then the print statement should print the modified statement(which it does).

Code:

class _HomeState extends State<Home> {
  var _input;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('hello')),
      body: Column(
        children: [
          TextFormField(
            initialValue: 'initial value ',
            onChanged: (text) {
              setState(() {
                _input = text;
              });
            },
          ),
          RaisedButton(
            onPressed: () {
              print(_input);
            },
            child: Text('Press me'),
          )
        ],
      ),
    );
  }
}

Output if I dont edit the TextFormField

flutter: null

Output if I edit the TextFormField

flutter: initial value test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私野 2025-02-11 04:13:15

如果您使用textedingcontroller,那会更好。

 final controller =
      TextEditingController.fromValue(TextEditingValue(text: 'initial value '));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('hello')),
      body: Column(
        children: [
          TextFormField(
            controller: controller,
            onChanged: (text) {},
          ),
          RaisedButton(
            onPressed: () {
              print(controller.text);
            },
            child: Text('Press me'),
          )
        ],
      ),
    );
  }

有关 textededingingcontroller

如果您仍然使用该变量,请在变量上初始化文本并使用它。

 String _input = 'initial value ';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('hello')),
      body: Column(
        children: [
          TextFormField(
            initialValue: _input,
            onChanged: (text) {
              setState(() {
                _input = text;
              });
            },
          ),
          RaisedButton(
            onPressed: () {
              print(_input);
            },
            child: Text('Press me'),
          )
        ],
      ),
    );
  }

另外,您可以使其无效,并检查它是否为null,然后提供默认值,在这种情况下似乎不需要。

String? _input;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('hello')),
      body: Column(
        children: [
          TextFormField(
            initialValue: 'initial value ',
            onChanged: (text) {
              setState(() {
                _input = text;
              });
            },
          ),
          RaisedButton(
            onPressed: () {
              print(_input ?? 'initial value ');
            },
            child: Text('Press me'),
          )
        ],
      ),
    );
  }

It would be better if you use TextEditingController.

 final controller =
      TextEditingController.fromValue(TextEditingValue(text: 'initial value '));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('hello')),
      body: Column(
        children: [
          TextFormField(
            controller: controller,
            onChanged: (text) {},
          ),
          RaisedButton(
            onPressed: () {
              print(controller.text);
            },
            child: Text('Press me'),
          )
        ],
      ),
    );
  }

More about TextEditingController.

If you still use the variable, initialize the text on variable and use it.

 String _input = 'initial value ';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('hello')),
      body: Column(
        children: [
          TextFormField(
            initialValue: _input,
            onChanged: (text) {
              setState(() {
                _input = text;
              });
            },
          ),
          RaisedButton(
            onPressed: () {
              print(_input);
            },
            child: Text('Press me'),
          )
        ],
      ),
    );
  }

Also you can make it nullable and check if it is null then provide default value which is seeming unnecessary in this case.

String? _input;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('hello')),
      body: Column(
        children: [
          TextFormField(
            initialValue: 'initial value ',
            onChanged: (text) {
              setState(() {
                _input = text;
              });
            },
          ),
          RaisedButton(
            onPressed: () {
              print(_input ?? 'initial value ');
            },
            child: Text('Press me'),
          )
        ],
      ),
    );
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文