Variable value gets null outside setState() in Flutter

                              TextField(
                                        style: TextStyle(
                                            fontFamily: "Averta",
                                            fontWeight: FontWeight.bold,
                                            fontSize: 16,
                                            color: Colors.black),
                                        enableSuggestions: true, 

                                        onChanged: (value) {
                                          setState(() {
                                            final title = value;
                                            print(title);
                                          });
                                        },

                                        decoration: InputDecoration(
                                          border: InputBorder.none,
                                          contentPadding:
                                              EdgeInsets.all(18),
                                          hintText: "Enter Task Title",
                                          hintStyle: TextStyle(
                                              fontFamily: "Averta",
                                              fontWeight: FontWeight.normal,
                                              fontSize: 12,
                                              color: Colors.grey.shade300),
                                        ),
                                      ),

Here,I have a title variable which I update with onChanged() function. Here the print() function shows exact change in value which I do in TextField. But I don't want to pass it here, I want to take some more data and then pass it when I click the CREATE button here-

                            InkWell(
                                    
                                    onTap: () {
                                      print(title);
                                            setState(() {
                                              DatabaseHelper _dbhelper =
                                                  DatabaseHelper();
                                              Task _newTask = Task(
                                                  title: title,
                                                  year: year,
                                                  month: month,
                                                  day: daye,
                                                  hour: hour,
                                                  minute: minute,
                                                  weekday: weekday);
                                              _dbhelper
                                                  .insertTask(_newTask);
                                            });
                                      }
                                    

Here the print statement shows "null",but I want to use the value which was updated above in TextField. How can I achieve this in Flutter?



Read more here: https://stackoverflow.com/questions/65719502/variable-value-gets-null-outside-setstate-in-flutter

Content Attribution

This content was originally published by Durgesh Kudalkar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: