Flutter - dispose() Method with Example Last Updated : 04 May, 2025 Comments Improve Suggest changes Like Article Like Report Dispose is a method triggered whenever the created object from the stateful widget is removed permanently from the widget tree. It is generally overridden and called only when the state object is destroyed. Dispose releases the memory allocated to the existing variables of the state. It is only used in a stateful widget because you cannot change the state of a stateless widget.Syntax:void dispose(){ //... super.dispose(); //...}If you create objects and do not free the memory used by them before their destruction, there is be chance your application will through a memory leakage in the app store or the play store. It is the exit point of the Stateful Widget. Execution of the dispose method is done in the end when the state object has built itself enough times, and now there is no need for it to build again. It is the last stage of a widget lifecycle; after this, a state object is destroyed completely. Situations where you need to call your dispose() method could be turning off the notifications, unsubscribing, shutting off the animations, etc.Implementation: Dart // ignore_for_file: avoid_unnecessary_containers import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( title: const Text('GeeksforGeeks'), backgroundColor: Colors.green, ), body: const FirstScreen())); } } class FirstScreen extends StatelessWidget { const FirstScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { // Avoid unnecessary containers return Container( child: Center( child: ElevatedButton( onPressed: () => Navigator.of(context) .push(MaterialPageRoute(builder: (context) => const NewScreen())), child: const Text('Move to the next screen'), ) // RaidedButton is deprecated and would be removed in the coming versions. // Use ElevatedButton instead. // child: RaisedButton( // color: Colors.green, // onPressed: () => Navigator.of(context) // .push(MaterialPageRoute(builder: (context) => const NewScreen())), // child: const Text('Move to next screen'), // ), )); } } class NewScreen extends StatefulWidget { const NewScreen({Key? key}) : super(key: key); @override State<NewScreen> createState() => _NewScreenState(); } class _NewScreenState extends State<NewScreen> { TextEditingController textEditingController = TextEditingController(); @override void dispose() { textEditingController.dispose(); // ignore: avoid_print print('Dispose used'); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('New Screen'), backgroundColor: Colors.green, ), // Avoid unnecessary containers body: Container( child: const Center( child: Padding( padding: EdgeInsets.symmetric(horizontal: 20.0), child: Text('This is the new screen')), )), ); } } Output:In the above example, the dispose method is called only and only once we exit the new screen. Comment More infoAdvertise with us Next Article Flutter - dispose() Method with Example R rharshitaaa21 Follow Improve Article Tags : Dart Flutter Flutter Flutter-Basics Similar Reads Flutter - Loading Kit Widget with Example In every android application, you have seen the loading bars, which are used when something is loading such as loading data, loading the screen, and fetching the data. The same flutter gives you a widget Flutter loading kit. We simply use the widget and use it in our project. A sample video is given 2 min read Flutter - Implementing Swipe to Dismiss Feature The Swipe to dismiss feature is used by us in many mobile apps. In this article, we will create a list of items and implement a swipe to dismiss in it. When we want to dismiss the content using swipe then we can do this with the help of swipe to dismiss widget in a flutter. Generally, we use this wh 2 min read Flutter - Dismissible Widget The Dismissible widget in Flutter is used to create items that can be dismissed by swiping them off the screen. It's commonly used in lists or grids where you want to provide a way for users to remove items with a swipe gesture. In this article, we are going to implement the Dismissible widget and e 4 min read Dart Extension Methods in Flutter Dart is a modern, object-oriented programming language used in web and mobile development. One of its most powerful features is extension methods. With extension methods, developers can extend the functionality of a class without modifying the class itself. This article will discuss extension method 4 min read Flutter - Deleting Data On The Internet In this article, we will explore the process of deleting data on the internet. Before deleting data on the internet, we will fetch the data first and then will delete the data.Steps to implement Deleting Data on the InternetStep 1 : Create a new flutter applicationCreate a new Flutter application us 4 min read Like