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(); }
The Dispose method performs all object cleanup, so the garbage collector no longer needs to call the objects' Object. Finalize override. Therefore, the call to the SuppressFinalize method prevents the garbage collector from running the finalizer. If the type has no finalizer, the call to GC.
Remember to dispose of the TextEditingController when it is no longer needed. This will ensure we discard any resources used by the object. This example creates a TextField with a TextEditingController whose change listener forces the entered text to be lower case and keeps the cursor at the end of the input.
The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
dispose method used 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 the stream controller. Otherwise, your app may get a warning from the PlayStore and AppStore about memory leakage.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With