I don't need to do many things with TextEditingController but want to show the initial text. And I feel like creating StatefulWidget is too much for that. Here's what I want my code looks like
// In StatelessWidget
TextField(
controller: TextEditingController(),
)
But every tutorials and blog posts I've seen use TextEditingController in StatefulWidget and dispose them in the dispose method. But I can't dispose them if I use like the above
Second, remember to dispose of the TextEditingController inside dispose() when it is no longer needed. This will ensure we discard any resources used by the object. There is no need to dispose of it while in use. Remember: the controller is not there to notify listeners of the changes inside the text input field.
A TextEditingController can also be used to provide an initial value for a text field. If you build a text field with a controller that already has text, the text field will use that text as its initial value.
A widget is either stateful or stateless. If a widget can change—when a user interacts with it, for example—it's stateful. A stateless widget never changes. Icon , IconButton , and Text are examples of stateless widgets.
In Flutter, this can be done using TextEditingController . First, create a TextEditingController and set it as a controller property of your TextField widget. In this example, I have added an extra Button and Text widget which will show the added text when you click the “Show Text” button.
In this post, you will learn about when to use stateful widgets vs stateless widgets while building a Flutter app. Use Stateful widgets in case of the following scenarios. Note that the StatefulWidget instances themselves are immutable and store their mutable state either in separate State objects that are created by the createState method.
The problem is, it's exactly what you are trying to. TextEditingController is a class instance that should be kept between renders. But by storing it into a StatelessWidget you basically recreate it after every update. You should instead convert your widget to Stateful.
If you want to use TextEditingController, there is no way around except to use a StatefulWidget if you want to avoid memory leaks. However, if you see alot of boilerplate in this approach, you can use HookWidget ( flutter_hooks) which gives you access to TextEditingController in a simple way and disposes it for you,here is a comparison:
Using TextFormField in Stateless widget is very difficult in flutter You could dispose It If you pass It from constructor, and dispose It from parent, but I'm confused why not just use StatefulWidget. @E.Benedos what memory leaks if Dart is garbage collected?
If you want to use TextEditingController
, there is no way around except to use a StatefulWidget
if you want to avoid memory leaks.
However, if you see alot of boilerplate in this approach, you can use HookWidget
(flutter_hooks) which gives you access to TextEditingController
in a simple way and disposes it for you,here is a comparison:
using StatefulWidget
:
class Test extends StatefulWidget {
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
TextEditingController controller;
FocusNode focusNode;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: 200,
height: 200,
color: Colors.red,
child: TextField(
focusNode: focusNode,
controller: controller,
),
),
),
);
}
@override
void initState() {
controller = TextEditingController();
focusNode = FocusNode();
super.initState();
}
@override
void dispose() {
controller.dispose();
focusNode.dispose();
super.dispose();
}
}
using HookWidget
:
class Test extends HookWidget {
@override
Widget build(BuildContext context) {
final focusNode = useFocusNode();
final controller = useTextEditingController();
return Scaffold(
body: Center(
child: Container(
width: 200,
height: 200,
color: Colors.red,
child: TextField(
focusNode: focusNode,
controller: controller,
),
),
),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With