How to do something before object destruction in Dart?
Asked Answered
D

3

35

In Java we can do something like override finalize(), in C++ we can do something like ~Someclass(),

But How can I do it in dart, I read the doc in https://www.dartlang.org/ but did not find answers.

Drag answered 27/7, 2015 at 5:49 Comment(1)
If you are using angular-dart, DetachAware can help you. Implement your component from DetachAware and override detach method.Chromyl
C
33

This is not supported. There is nothing like a destructor in Dart.
JS garbage collector doesn't provide a way to implement this. See also https://mcmap.net/q/450397/-how-do-we-delete-an-object-in-dart and https://github.com/dart-lang/sdk/issues/3691

Comines answered 27/7, 2015 at 5:50 Comment(0)
L
6

Dart now provides finalizers, as of Dart 2.17.

Leporide answered 16/8, 2022 at 0:12 Comment(0)
Z
0

I don't know if this is relevant, but Flutter provides dispose method, which works similar to a deconstructor, it lets you dispose any object or instance methods, e.g.

  • Subscription (which your don' t want to after your page is gone)

just override this method after your build method to dispose any methods.

Zamia answered 22/12, 2023 at 10:23 Comment(1)
dispose() is only available on State objects. and it is called by the framework when State object is removed from the element treeGoingson

© 2022 - 2024 — McMap. All rights reserved.