Why do we use dispose() method in dart code?

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-21 12:05:44

问题


Why are we using dispose() method? I'm little confused about it. what will be issue occurs If we don't use it and what's the benefit of using it?

@override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

回答1:


dispose method use to release the memory allocated to variables when state object is removed.

For example, if you are using a stream in your application then you have to release memory allocated to streamController. Otherwise your app may get warning from playstore and appstore about memory leakage.




回答2:


The main purpose is to get a callback where in you can free-up all your resources.

If you have initialized any resource in a State, it is important that you close or destroy that resource when that state is disposed.

For e.g: If you are creating a stream in initState of your StatefullWidget, then it is important that you close that stream in dispose method of that state or else it will cause memory leak.

For more details you can refer following comments which I got from the source code of the dispose method of the StatefulWidget:

Called when this object is removed from the tree permanently. The framework calls this method when this [State] object will never build again. After the framework calls [dispose], the [State] object is considered unmounted and the [mounted] property is false. It is an error to call [setState] at this point. This stage of the lifecycle is terminal: there is no way to remount a [State] object that has been disposed. Subclasses should override this method to release any resources retained by this object (e.g., stop any active animations). {@macro flutter.widgets.subscriptions} If you override this, make sure to end your method with a call to super.dispose(). See also: * [deactivate], which is called prior to [dispose].

Or you can refer the docs: https://api.flutter.dev/flutter/widgets/State/dispose.html

So basically dispose is called when that current state will never be used again. So, if you are having any listeners that are active in that state then they can cause memory leaks and so you should close them.




回答3:


dispose() method called automatically from stateful if not defined.

In some cases dispose is required for example in CameraPreview, Timer etc.. you have to close the stream.

When closing the stream is required you have to use it in dispose method.

dispose() is used to execute code when the screen is disposed. Equal to onDestroy() of Android.

Example:

@override
void dispose() {
  cameraController?.dispose();
  timer.cancel();
  super.dispose();
}



回答4:


dispose() method is called when this object is removed from the tree permanently.

For more information, you can refer official Docs: https://api.flutter.dev/flutter/widgets/State/dispose.html

Implementation Example:

@protected
@mustCallSuper
void dispose() {
  assert(_debugLifecycleState == _StateLifecycle.ready);
  assert(() {
    _debugLifecycleState = _StateLifecycle.defunct;
    return true;
  }());
}




回答5:


In general terms dispose means freeing the resources before the related object is removed from the focus. By focus, I mean from widget tree or navigation stack whichever is relevant.

When you call dispose on a widget state, the associated widget is supposed to be unmounted which means the widget will never rebuild.

Called when this object is removed from the tree permanently.

The framework calls this method when this State object will never build again.

After the framework calls dispose, the State object is considered unmounted and the mounted property is false. It is an error to call setState at this point.

This stage of the lifecycle is terminal: there is no way to remount a State object that has been disposed.

When you call dispose on a bloc, the bloc is supposed to be closing the event and state stream. Update: New Bloc does not have dispose, instead it has close.

So, This is what dispose basically means.

TL;DR In general, it means releasing the resources held by the associated instance/object.

The specific meaning of dispose, however, changes with the types of the object on which this method is called.



来源:https://stackoverflow.com/questions/59558604/why-do-we-use-dispose-method-in-dart-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!