简体   繁体   English

无状态小部件会自行处理吗?

[英]Do stateless widgets dispose on their own?

I created a PostUpdaterWidget extending StatelessWidget which makes use of TextEditingControllers for testing out implementation of Bloc Pattern.我创建了一个扩展StatelessWidgetPostUpdaterWidget ,它利用TextEditingControllers来测试 Bloc 模式的实现。

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 .但是,没有方法可以override StatelessWidgetdispose函数。

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.我想过创建自己的 dispose 函数来处理所使用的控制器,只需为那些将使用此小部件的人创建此小部件的变量,以便我可以调用 dispose 函数。

But I want to know first whether I really need to do that, or this StatelessWidget actually disposes on its own.但我想首先知道我是否真的需要这样做,或者这个 StatelessWidget 实际上是自己处理的。

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.或者就这样吧,因为它可能会自行处理这些控制器,这样我就不必担心内存泄漏。

This question seems to indicate that objects are not disposed when the StatelessWidget gets destroyed, at least not immediately. 这个问题似乎表明当StatelessWidget被销毁时,对象不会被销毁,至少不会立即销毁。 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.在任何情况下,当您使用TextEditingController (或维护任何可变状态)时,您应该使用StatefulWidget并将StatefulWidget保留在State类中。 The State class has a dispose() method that you can use (as you mentioned in your question). State类有一个您可以使用的dispose()方法(正如您在问题中提到的)。

Otherwise, if you use a StatelessWidget , you lose your state every time the UI gets rebuilt.否则,如果您使用StatelessWidget ,则每次重建 UI 时都会丢失您的状态。 StatefulWidgets keep their state across rebuilds because the state is in the State class, not in the widget. StatefulWidgets在重建过程中保持它们的状态,因为状态在State类中,而不是在小部件中。 See also this answer .另请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM