无状态小部件会自行处置吗? [英] Do stateless widgets dispose on their own?

查看:136
本文介绍了无状态小部件会自行处置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个PostUpdaterWidget扩展StatelessWidget,它使用TextEditingControllers来测试Bloc Pattern的实现.

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);
  }

在许多示例中,我已经看到应该放置控制器.但是,在StatelessWidget中没有override一个dispose函数的方法.

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.

我曾考虑过创建自己的dispose函数来处理所使用的控制器,并且只为将要使用该控件的用户创建此窗口小部件的变量,以便我可以调用dispose函数.

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.

但是我想首先知道我是否真的需要这样做,或者这个StatelessWidget实际上是自己处理的.

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.

推荐答案

这个问题似乎表明StatelessWidget被销毁后,物体没有被处置,至少不是立即被销毁.无论如何,当您使用TextEditingController(或保持任何可变状态)时,都应使用StatefulWidget并将状态保留在State类中. State类具有您可以使用的dispose()方法(如您在问题中所述).

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).

否则,如果使用StatelessWidget,则每次重建UI时都会丢失状态. StatefulWidgets在整个重建过程中保持其状态,因为状态在State类中,而不是在小部件中.另请参见此答案.

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.

这篇关于无状态小部件会自行处置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆