Flutter adding text into TextField controller

Viewed 269

When i use this code it works correctly, the controller.text is updated.

textController.addListener(() {
          setState(() {
            textController.text = analyzeText;
          });
        });

But when i try to add end of text, it crash does not give error. Why?

textController.addListener(() {
          setState(() {
            textController.text += ' ' + analyzeText;
          });
        });

2 Answers

It seems to me that you are creating a never ending loop, possibly creating a memory leak of sorts. You have a listener listening for changes in a controller, and inside that listener you are making a change to that very same controller. So once it starts, it never stops. That is very likely to cause a crash.

This is not working because of StackOverflowError. It is thrown, when call stack exceeds due to excessive deep or infinite recursion.

  setState(() {
            textController.text += ' ' + analyzeText;
          });

The above code continuously changing the values controller which will be going to replace the widget inside WidgetTree, as Flutter is immutable it will create Widget again and again in memory, so with this, you will have the above error.

Related