Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to use TextEditingController in StatelessWidget in Flutter?

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

like image 659
goodonion Avatar asked Apr 25 '20 12:04

goodonion


People also ask

Should I dispose TextEditingController?

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.

Why use TextEditingController in flutter?

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.

What is the difference between a Statelesswidget and a Statefulwidget in flutter?

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.

How do you use TextField in flutter?

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.

When to use stateful widgets vs stateless widgets in flutter?

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.

Why can't I store texteditingcontroller in a statelesswidget?

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.

How to use texteditingcontroller in flutter?

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:

Is it possible to use textformfield in stateless widget?

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?


1 Answers

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,
          ),
        ),
      ),
    );
  }
}
like image 152
Haidar Avatar answered Oct 20 '22 09:10

Haidar