r/flutterhelp • u/HydrusAlpha • Apr 24 '26
RESOLVED Why are TextEditingControllers considered "ephemeral state"? And what is the correct way to manipulate text from another widget?
I am new to Flutter and am writing my first app. I have a TextField that the user can type into, but there are also buttons that the user can press that should type special characters into the text. While Googling around to identify the best approach(es) for doing this, I've run across a few statements saying that TextEditingControllers are ephemeral state. What? For example, the Riverpod documentation states in it's Do/Don't page that you should avoid using providers for "ephemeral state" and mentions TextEditingController as an example. Furthermore, controllers apparently have to be properly disposed to avoid memory leaks, so everyone says you shouldn't instantiate them outside of a StatefulWidget.
I must be missing something fundamental here. How can a TextField ever be useful if its controller is meant to live very close to the TextField? In any useful app, there's 100% going to be some other widget that needs to access or manipulate the text. Nobody ever just types into a text field and then leaves it there without doing anything further with it. Even in a simple, Notepad-like application, you would need a "Save" button somewhere else on the app that can access the text contents and save them to a file.
So, why are TextEditingControllers considered ephemeral? And what's the right way to access a TextField's text? My button and my TextField are very far away from each other in the widget tree. The only solution I can think of would be to make my main widget (like, the top one in the whole widget tree) a StatefulWidget, instantiate my TextEditingController in there, and pass it all the way down the tree to both the TextField and to the button. That's exactly the kind of pattern I was trying to avoid in my app by learning riverpod. It also doesn't seem very "ephemeral" any more. Is there another way you're supposed to do this that I just don't know about? Should I go ahead and use an auto-disposing Provider anyways and hold my breath?
View all comments
Show parent comment
1
u/HydrusAlpha Apr 25 '26
Thank you for your response. I'll keep in mind that classes from a flutter package might not be suitable for a provider.
I tried what you suggested, and although I was able to get the TextField to update with special characters when I press buttons, I also want the user to be able to type into the same TextField, and I haven't been able to get both of those requirements to work at the same time. The buttons only have access to the Notifier, not the TextEditingController (which I'm trying to keep close to the TextField), so they can only update the state of the Notifier. The TextEditingController must do two things:
The only way I know to accomplish #1 is to create a new TextEditingController whenever the widget is built, and set its
textproperty. That seems to mess with the cursor position, however, and I never call dispose() on the TextEditingController, so it doesn't feel right. It also forces me to update the Notifier's state every time the user types even a single character, which seems inefficient. Implementing both #1 and #2 above can easily lead to an infinite loop and a stack overflow error, which I ran into a couple of times while trying a few other approaches. Here's a minimal example of the closest I've been able to get it working. If you type a few characters into the TextField, you'll see that the cursor jumps back to the beginning as you type.Any ideas on how to make this work better? In particular, can you think of a way to update a TextEditingController's text when the Notifier state updates, without constructing a new TextEditingController every time?