How to prevent other widgets from staying active when leaving them in flutter

607 Views Asked by At

I have an app with many pages/interfaces and most of them are getting the data from an api, when i leave a page and go to another, the previous one stays active forever. I noticed that when prenting the data result on every page and i noticed even when i leave the page before finishing it job, it keep fetching the data at the same time with the next page that i opened. This caused me many problems when checking many pages one after another and made the app lag or close connection with the api. I want to know how to prevent this from happenening

This is how i go from one page to another

 Navigator.of(context).push(
              MaterialPageRoute(builder: (BuildContext context)=>Dashboard()
              ));
1

There are 1 best solutions below

0
On

You need to dispose the previous page. You can do it by pushReplacement route state. It will dispose all your previous page's methods which are currently running.

 Navigator.of(context).pushReplacement(
          MaterialPageRoute(builder: (BuildContext context)=>Dashboard()
 ));