subject 2.0.0 copy "subject: ^2.0.0" to clipboard
subject: ^2.0.0 copied to clipboard

Automatically generates an observable interface for any class. Observer Pattern implementation for Dart, generalized using mixins and multiple alternative interfaces.

example/main.dart

import 'package:subject/observer.dart';

void main() {
  final subject = Subject<String>();

  final observer =
      Observer<String>((subject, state) => print('Observer 1: $state'));
  final streamObserver = Observer.stream<String>()
    ..listen((state) => print('Observer 2: $state'));

  subject.attach(observer);
  subject.attach(streamObserver);

  subject.notify('Hello World!');
  print(
      'There are ${subject.observers.length} observers attached to the subject.');

  print('Detaching observer...');
  subject.detach(observer);

  subject.notify('Hello World, again!');

  /* [Output]
    Observer 1: Hello World!
    Observer 2: Hello World!

    There are 2 observers attached to the subject.
    Detaching observer...

    Observer 2: Hello World, again!
  */
}
3
likes
150
points
596
downloads
screenshot

Documentation

API reference

Publisher

verified publisherdrafakiller.com

Weekly Downloads

Automatically generates an observable interface for any class. Observer Pattern implementation for Dart, generalized using mixins and multiple alternative interfaces.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

rxdart

More

Packages that depend on subject