flutter_pubsub 0.0.1 copy "flutter_pubsub: ^0.0.1" to clipboard
flutter_pubsub: ^0.0.1 copied to clipboard

A lightweight and efficient state management solution for Flutter, implementing the Publisher-Subscriber pattern with minimal boilerplate and zero dependencies.

example/lib/main.dart

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

// State management using Pub
class Counter extends Pub {
  int _count = 0;

// IMPORTANT: Wrap state objects in a `get` method.
  int get count => get(_count);

  void increment() {
    _count++;
    notifyListeners();
  }

  void decrement() {
    _count--;
    notifyListeners();
  }
}

// Create a singleton instance
final counter = Counter();

// Main app
void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'PubSub Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: const CounterScreen(),
    );
  }
}

// Counter screen with multiple widgets subscribing to the same state
class CounterScreen extends StatelessWidget {
  const CounterScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('PubSub Counter'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // First subscriber - shows count
            Sub(
              (_) => Text(
                'Count: ${counter.count}',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
            ),
            const SizedBox(height: 20),
            // Second subscriber - shows even/odd
            Sub(
              (_) => Text(
                'Number is ${counter.count.isEven ? "even" : "odd"}',
                style: Theme.of(context).textTheme.titleLarge,
              ),
            ),
            const SizedBox(height: 20),
            // Controls
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FloatingActionButton(
                  onPressed: counter.decrement,
                  child: const Icon(Icons.remove),
                ),
                const SizedBox(width: 20),
                FloatingActionButton(
                  onPressed: counter.increment,
                  child: const Icon(Icons.add),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
150
points
15
downloads

Publisher

unverified uploader

Weekly Downloads

A lightweight and efficient state management solution for Flutter, implementing the Publisher-Subscriber pattern with minimal boilerplate and zero dependencies.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on flutter_pubsub