disposito 1.0.1 copy "disposito: ^1.0.1" to clipboard
disposito: ^1.0.1 copied to clipboard

Analyzer plugin for object lifetimes: fields annotated with @Disposable must be cleaned up by the class that declares them, objects that own a lifecycle must not be created during a Flutter build, and [...]

example/lib/example.dart

// Run `dart pub get && dart analyze` in this directory to see the rules fire.
//
// Expected output: exactly one warning per line marked "reported" below.
import 'package:disposito/disposito.dart';

/// A resource that has to be released.
class Connection {
  void close() {}
  void detach() {}
}

class GoodService {
  @Disposable()
  final Connection _primary = Connection();

  @Disposable(#detach)
  final Connection _secondary = Connection();

  void doSomething() {
    print('$_secondary');
  }

  /// Both fields are cleaned up here, so neither is reported.
  void dispose() {
    _primary.close();
    _secondary.detach();
  }
}

class LeakyService {
  // reported: missing_dispose, nothing in this class ever closes it.
  @Disposable()
  final Connection _connection = Connection();

  void read() {
    print(_connection);
  }
}

class SuppressedService {
  // Diagnostics are suppressed with the plugin name as a prefix. The rule
  // reports on the field's name, so the comment goes directly above that
  // line, below the annotation.
  @Disposable()
  // ignore: disposito/missing_dispose
  final Connection _connection = Connection();

  void read() {
    print(_connection);
  }
}
4
likes
150
points
86
downloads

Documentation

API reference

Publisher

verified publisherarxdeus.dev

Weekly Downloads

Analyzer plugin for object lifetimes: fields annotated with @Disposable must be cleaned up by the class that declares them, objects that own a lifecycle must not be created during a Flutter build, and a State must not hand itself out of its own dispose or build a late field during teardown.

Repository (GitHub)
View/report issues

Topics

#analyzer-plugin #lints #static-analysis #memory

Funding

Consider supporting this project:

ko-fi.com

License

MIT (license)

Dependencies

analysis_server_plugin, analyzer, analyzer_plugin, analyzer_plugin_toolkit

More

Packages that depend on disposito