When and how to release resources in Dart?

Viewed 218

What is a good way to close/release resources, when we do not have destructors in Dart?

For example take a Database class:

class Database {

  final String filename;

  RandomAccessFile _file;  

  Database(this.filename) {
    _file = File(filename).openSync();
  }

  // more reading, writing methods...
}

We surely should call _file.close() somewhere, and we can add a method to the Database class to do so. This just seems to shift the problem to another class, not solve it.

I guess I am looking for some kind of c++ RAII-ish thingy here, but with no clean-up methods I can only think of trivial short-lived objects to support something similar.

0 Answers
Related