notification_dispatcher 0.3.2 copy "notification_dispatcher: ^0.3.2" to clipboard
notification_dispatcher: ^0.3.2 copied to clipboard

Inspired by Apple's NotificationCenter. Passes information around to registered observers.

Build status Pub package License: MIT

Inspired by Apple's NotificationCenter. Passes information around to registered observers.

Installing #

Add the following line to your pubspec.yaml file.

notification_dispatcher: ^0.3.2

Flutter Example #

void main() {
  runApp(const App());
}

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  int _count = 0;

  @override
  void initState() {
    super.initState();
    NotificationDispatcher.instance.addObserver(
      this,
      name: 'observerName',
      callback: (_) => _incrementCount(),
    );
  }

  @override
  void dispose() {
    NotificationDispatcher.instance.removeObserver(this);
    super.dispose();
  }

  void _incrementCount() {
    setState(() {
      _count++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_count',
                style: Theme.of(context).textTheme.headline4,
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () =>
              NotificationDispatcher.instance.post(name: 'observerName'),
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}

Equatable #

To set an Equatable-extended class as an observer, use the NotificationDispatcherEquatableObserverMixin mixin and add the instanceKey property to its props property.

class SomeClass extends Equatable with NotificationDispatcherEquatableObserverMixin {
  @override
  List<Object?> get props => [...super.props, instanceKey];
}
5
likes
160
points
79
downloads

Publisher

unverified uploader

Weekly Downloads

Inspired by Apple's NotificationCenter. Passes information around to registered observers.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on notification_dispatcher