Do stateless widgets dispose on their own?
Asked Answered
C

1

50

I created a PostUpdaterWidget extending StatelessWidget which makes use of TextEditingControllers for testing out implementation of Bloc Pattern.

final _usernameController = TextEditingController();
  final _contentController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        TextField(
          controller: _usernameController,
          decoration: InputDecoration(hintText: "Post Username"),
        ),
        TextField(
          controller: _contentController,
          decoration: InputDecoration(hintText: "Post Content"),
        ),
        Container(
          height: 16,
        ),
        RaisedButton(
          child: Text("Update Post"),
          onPressed: () => _updatePost(context),
        )
      ],
    );
  }

  _updatePost(BuildContext context) {
    print("Processing Post Update");
    String username = _usernameController.text.trim();
    String content = _contentController.text.trim();

    Post post = new Post();
    post.id = id;
    post.username = username;
    post.content = content;

    id += 1;

    print("Dispatching Post Update");
    BlocProvider.of<PostBloc>(context).updatePost(post);
  }

I have seen in a lot of examples that controllers should be disposed. However there is no method to override a dispose function in a StatelessWidget.

I have thought of creating its own dispose function to dispose the controllers used, and just create a variable of this widget for those that will use this widget so that I can call the dispose function.

But I want to know first whether I really need to do that, or this StatelessWidget actually disposes on its own.

Should I proceed with my idea? Or just leave it be, since it might be disposing these controllers on its own, so that I should not be concerned of memory leaks.

Charged answered 6/4, 2019 at 13:44 Comment(4)
Since, stateless widget doesnt contain state, there is no method like that. I think its better to use stateful widget with controllers like that. You should read this: docs.flutter.io/flutter/widgets/StatelessWidget-class.htmlSanious
And this: docs.flutter.io/flutter/widgets/StatefulWidget-class.htmlSanious
@Sanious I think it's safer too but is it really mandatory for proper disposal or will the StatelessWidget do it by itself when garbage collected?Dugas
@NicolasDion-Bouchard - I think there is a danger of circular references here. When you set up your TextEditing controller. it will have references to other data object. Garbage collection won't occur on those objects because they still hold a reference to something else. This will probably cause a memory leak. I would just convert to a Stateful Widget and dispose of everything explicitly. AAMOF, I just did that in my own project.Kalong
E
36

This question seems to indicate that objects are not disposed when the StatelessWidget gets destroyed, at least not immediately. In any case, when you are using a TextEditingController (or maintaining any mutable state), then you should use a StatefulWidget and keep the state in the State class. The State class has a dispose() method that you can use (as you mentioned in your question).

Otherwise, if you use a StatelessWidget, you lose your state every time the UI gets rebuilt. StatefulWidgets keep their state across rebuilds because the state is in the State class, not in the widget. See also this answer.

Eastern answered 22/1, 2020 at 13:5 Comment(4)
Say you're subscribing to a listener in a stateless widget, is a new listener subscribed to each time the UI is rebuilt?Historiated
@BrandonPillay, I'm not really sure. Sounds like an interesting question. If you ask this as a new SO question with a code sample, can you put a link to it here?Eastern
Can we use something like mobx or bloc (any state management tool) to manage and dispose controllers without making widget stateful? And if we can, which technically seems possible should we. and also what if i make a generic function to use with different controllers, would this still be safe?Herv
@BrandonPillay, Yeees ... i just test it with Timer.periodic , in general StatelessWidget doesn't have the ability to release resources (Except its inner painting objects) , so any type of streams (timers, textEditing controllers, files buffers ...etc) will not closed while stateless widget disposing, be carefulBoracite

© 2022 - 2024 — McMap. All rights reserved.