manageAndReturnDisposable method

  1. @mustCallSuper
  2. @override
Disposable manageAndReturnDisposable (Disposable disposable)
@mustCallSuper, override

Automatically dispose another object when this object is disposed.

This method is an extension to manageDisposable and returns the passed in Disposable in addition to handling its disposal. The method should be used when a variable is set and should conditionally be managed for disposal. The most common case will be dealing with optional parameters:

 class MyDisposable extends Disposable {
   // This object also extends disposable
   MyObject _internal;

   MyDisposable({MyObject optional}) {
     // If optional is injected, we should not manage it.
     // If we create our own internal reference we should manage it.
     _internal = optional ??
         manageAndReturnDisposable(new MyObject());

   // ...

The parameter may not be null.

Use manageAndReturnTypedDisposable instead. One will need to update to DisposableManagerV7 or above for this.


Disposable manageAndReturnDisposable(Disposable disposable) {
  _throwOnInvalidCall('manageAndReturnDisposable', 'disposable', disposable);

  return disposable;