1

I have declared var valueName; before my widget builder and after I set it with Firebase document value: valueName = widget.item.name; Then, i used valueName for initial value of a TextFormField and a setState in onChanged. But when i use valueName in my update to Firestore function, it load the first old value. I can't update this value.

This is my TextFormField code:

TextFormField(
                      initialValue: valueName,
                      onChanged: (input) {
                        setState(() {
                          this.valueName = input;
                          print(valueName);
                        });
                      },
                      keyboardType: TextInputType.name,
                      decoration: InputDecoration(
                          border: UnderlineInputBorder(),
                          filled: true,
                          icon: Icon(
                              Icons.person
                          ),
                          hintText: 'Inserisci il nome',
                          labelText: 'Nome'
                      ),
                    ),

1 Answers1

2

have you tried using TextEditingController ? you can use it to set your initial value as well as change the value anytime you want without setState.

class Temp extends StatefulWidget {
//constractor
// you are getting your value here
  @override
  _TempState createState() => _TempState ();
}

class _TempState extends State<Temp> with {
//declare here
var variable = widget.item.name;
// you can change this variable
M.M.Hasibuzzaman
  • 1,015
  • 5
  • 10
  • i tried it right now and it doesn't work, it seems that if i declare a variable is equal to widget.item.name, it can't be changed by anything else – Stefano Quarta Sep 08 '20 at 07:27
  • 1
    widget.item.name cant be changed..... u can declare another variable and then store the widget.item.name in another variable shown like this. widget.item.name is from another place. i dnt think u can change its value from here. But there is a package named Get in PUb.dev you can try that ``` class Temp extends StatefulWidget { //constractor // you are getting your value here @override _TempState createState() => _TempState (); } class _TempState extends State with { //declare here var variable = widget.item.name; // you can change this variable ``` – M.M.Hasibuzzaman Sep 09 '20 at 08:32