1

I am looking for a way to dispose some final things before my application closes. I want this to happen on web close, on native window close and on mobile close.

I thought I'd do it like this, but the flutter engines takes my widgets and ends the main function.

void main() {
  try {
    runApp(MyApp());
  }
  finally {
    customDisposeFunction(); // TODO: Oh no, this function is called right away!
  }
}

class MyApp extends StatelessWidget {
}

I have also tried to use flutter_window_close, but that does not dispose when I press the stop button in visual studio code.

I have also tried to watch via ProcessSignal, but that is not cross-platform at all.

I have also tried to make the app a StatefulWidget, but dispose in the State is not called (on windows) when closing down the window.

Are there some good ways to handle this cross-platform?

Marnix
  • 6,384
  • 4
  • 43
  • 78

2 Answers2

0

Yes, because finally will get a hit directly after you write it this way. Instead you should try to write it in dispose method of your main class (you have to make it Stateful though)

0

You can make your MyApp a StatefulWidget and override its dispose() method.

Roslan Amir
  • 1,141
  • 8
  • 16