Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

How and Where to Call Dispose in Flutter

I was reading on how to use dispose on flutter but I cannot figure it out. If I call the below pagedispose function after Navigator.pushReplacementNamed, I am always getting an error. If I call this function from another class page, I then get errors that it doesn't exist etc.

Would appreciate your guidance on how to clear all running functions etc on a page when transitioning to another Flutter page.

@override
void pagedispose(){   
    vtimer.cancel();   
    vcontroller.dispose();   
    super.dispose();   
 }

Thanks in advance.

question from:https://stackoverflow.com/questions/65863463/how-and-where-to-call-dispose-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the documentation

dispose method

Called when this object is removed from the tree permanently.

Regarding a page, the dispose method is called when the page is removed from the navigation stack. Here is a good explanation of Navigation.
When your widget (page) extends StatefulWidget, it's not mandatory but you can override the dispose method to execute additional instructions depending on your need. The method is called automatically when the page is being removed from navigation tree. Override the method as following

@override
void dispose() {
  // your desired instructions here

  super.dispose(); // This will free the memory space allocated to the page
}

Nonetheless, the method void pagedispose() cannot be overridden as it ain't a known method of StatefulWidget


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...