Home > OS >  How to delete data when navigator from screen to another in Flutter?
How to delete data when navigator from screen to another in Flutter?

Time:08-30

every one! My problem is when I store the data in the list coming from the api and I display it on one page and then go back to another page and when I go back to the old page, the data is still there. Why ??? How do I delete the data??

CodePudding user response:

In the code block that contains navigating logic, you can assign empty data to the data stored variable.

onTap: () {
  variableContainingData = ''; // assign empty data here

  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => NextPage()),
  );
}

CodePudding user response:

If you want to clear list initialy then you need to implement that functionality in onInit() method.

Bellow code would be helpful for you.

      List yourList = [];

      @override
      void initState() {
        yourList.clear();
        super.initState();
      }

and at the time of navigation you can also clear your list based on your requirement:

onPressed: () { 
    yourList.clear();
    Navigator.push(  
      context,  
      MaterialPageRoute(builder: (context) => SecondRoute()),  
);  

}

But if you want to add some product at the time of page initialization then you need to add that specific logic(based on requirement) also in onInit() method.

  • Related