Shutdown Hook in Dart?

Issue

I need make some action (dump statistical data) before the Dart program ends. The program continues after the method main ends (there are other isolates), so I cannot determine exactly the moment when the program finishes.

In Java, I can use java.lang.Runtime.addShutdownHook. Does similar feature exist in the Dart runtime?

Solution

It doesn’t, at least not directly in Dart. If you embed Dart VM into your program, you can register such hook for any isolate via the embedding API.

In the future, there will probably be some kind of isolates monitoring (which is useful in a lot of scenarios), so that you can get notified when an isolate dies (either normally or by an uncaught exception), but it isn’t there now.

Answered By – Ladicek

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.