dispose method Null safety

void dispose()

Release this handle's claim on the underlying Image. This handle is no longer usable after this method is called.

Once all outstanding handles have been disposed, the underlying image will be disposed as well.

In debug mode, debugGetOpenHandleStackTraces will return a list of StackTrace objects from all open handles' creation points. This is useful when trying to determine what parts of the program are keeping an image resident in memory.

Implementation

void dispose() {
  assert(!_disposed && !_image._disposed);
  assert(_image._handles.contains(this));
  _disposed = true;
  final bool removed = _image._handles.remove(this);
  assert(removed);
  if (_image._handles.isEmpty) {
    _image.dispose();
  }
}