Flutter adding text into TextField controller

103

Solution 1

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.

Solution 2

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.

Share:
103
mfturkcan
Author by

mfturkcan

Updated on December 24, 2022

Comments

  • mfturkcan
    mfturkcan over 1 year

    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;
              });
            });
    
    
  • mfturkcan
    mfturkcan over 3 years
    Yes you are right, the reason was listener. I fixed it .Thanks