Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save value of TextField when a value is inputted in Flutter

Tags:

flutter

I have this code in a stateful class The variables

String sth1 ;
String sth2 ;

I need these variables to each get a value from two TextFields once something is inputted. This is what I have

         TextField(
              autofocus: true,
              focusNode: f1,
              inputFormatters: [
                LengthLimitingTextInputFormatter(1),
              ],
              keyboardType: TextInputType.number,
              onChanged: (newVal) {
                if (newVal.length == 1) {
                    newVal = sth1;


                  f1.unfocus();
                  FocusScope.of(context).requestFocus(f2);
                }
like image 369
Taio Avatar asked Nov 16 '25 19:11

Taio


2 Answers

Your onChanged should have sth1 = newVal not newVal = sth1.

like image 142
Shady Aziza Avatar answered Nov 19 '25 08:11

Shady Aziza


Take a

TextEditingController tec = new TextEditingController();

And pass it to the TextField as controller

TextField(controller: tec);

And when you want to get the text just use

tec.text;

Thats it!

like image 21
Jaswant Singh Avatar answered Nov 19 '25 08:11

Jaswant Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!