Skip to main content

bloc_provider 1.1.0 copy "bloc_provider: ^1.1.0" to clipboard
bloc_provider: ^1.1.0 copied to clipboard

Provides bloc to descendant widget (O(1)), and the bloc is disposed appropriately by state which the bloc_provider holds internally.

example/example.dart

import 'dart:async';

import 'package:bloc_provider/bloc_provider.dart';
import 'package:material_ui/material_ui.dart';

class CounterBloc() implements Bloc {
  this {
    _countController.add(0);
  }

  final _countController = StreamController<int>();
  int _count = 0;

  Stream<int> get count => _countController.stream;

  void increment() {
    _count++;
    _countController.add(_count);
  }

  @override
  void dispose() {
    _countController.close();
  }
}

void main() => runApp(
  BlocProvider<CounterBloc>(
    creator: (context, bag) => CounterBloc(),
    child: const MaterialApp(home: CounterPage()),
  ),
);

class const CounterPage({super.key}) extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<CounterBloc>(context);
    return Scaffold(
      appBar: AppBar(title: const Text('Counter Example')),
      body: Center(
        child: StreamBuilder<int>(
          stream: bloc.count,
          initialData: 0,
          builder: (context, snap) => Text(
            'count: ${snap.data}',
            style: Theme.of(context).textTheme.headlineMedium,
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: bloc.increment,
        child: const Icon(Icons.add),
      ),
    );
  }
}
25
likes
160
points
5.16k
downloads

Documentation

API reference

Publisher

verified publishermono0926.com

Weekly Downloads

Provides bloc to descendant widget (O(1)), and the bloc is disposed appropriately by state which the bloc_provider holds internally.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter

More

Packages that depend on bloc_provider