notification_centre 0.0.5 copy "notification_centre: ^0.0.5" to clipboard
notification_centre: ^0.0.5 copied to clipboard

A lightweight, type-safe pub/sub library for Dart, inspired by Apple's `NotificationCenter` and Android's `EventBus`.

example/notification_centre_example.dart

import 'package:flutter/material.dart';
import 'package:notification_centre/notification_centre.dart';

const userSignedIn = NotificationName<String>('userSignedIn');

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: MainPage());
  }
}

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  late final ObservationToken _signInToken;
  String _loginState = 'Not signed in';

  @override
  void initState() {
    super.initState();
    _signInToken =
        NotificationCenter.shared.addObserver(userSignedIn, (userId) {
      setState(() => _loginState = 'Signed in as $userId');
    });
  }

  @override
  void dispose() {
    _signInToken.removeObserver();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(_loginState),
            TextButton(
              onPressed: () {
                NotificationCenter.shared.post(userSignedIn, 'user-123');
              },
              child: const Text('Sign in'),
            ),
          ],
        ),
      ),
    );
  }
}
5
likes
160
points
175
downloads

Documentation

API reference

Publisher

verified publisherjejouejeux.com

Weekly Downloads

A lightweight, type-safe pub/sub library for Dart, inspired by Apple's `NotificationCenter` and Android's `EventBus`.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter

More

Packages that depend on notification_centre