Home > database >  value of onChnaged giving me null
value of onChnaged giving me null

Time:06-04

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

CodePudding user response:

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'),
          )
        ],
      ),
    );
  }
  • Related