notification_dispatcher 0.3.2 notification_dispatcher: ^0.3.2 copied to clipboard
Inspired by Apple's NotificationCenter. Passes information around to registered observers.
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];
}