Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

value of onChnaged giving me null

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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'),
          )
        ],
      ),
    );
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading