Skip to content
Provides bloc to descendant widget (O(1)), and the bloc is disposed appropriately by state that the bloc_provider holds internally.
Branch: master
Clone or download
Latest commit 752a237 Jun 21, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Update rxdart Mar 9, 2019
lib Add `allowNull` argument to BlocProvider's `of` method Jun 11, 2019
test Fix test May 4, 2019
.gitignore Ignore /.history Mar 9, 2019
.metadata Add source Nov 29, 2018
CHANGELOG.md Add `allowNull` argument to BlocProvider's `of` method Jun 11, 2019
LICENSE Initial commit Nov 29, 2018
README.md Move badge Apr 27, 2019
analysis_options.yaml Fix analysis options Mar 9, 2019
bloc_provider.iml Add source Nov 29, 2018
pubspec.lock Upgrade dependencies Jun 21, 2019
pubspec.yaml Upgrade dependencies Jun 21, 2019

README.md

bloc_provider Codemagic build status

Provides BLoC(Business Logic Component) to descendant widget (O(1)), and the bloc is disposed automatically by the state which the bloc_provider holds internally.

Usage

1. Define some BLoC like this:

class CounterBloc implements Bloc {
  final _countController = BehaviorSubject<int>(seedValue: 0);
  final _incrementController = PublishSubject<void>();

  CounterBloc() {
    _incrementController
        .scan<int>((sum, _v, _i) => sum + 1, 0)
        .pipe(_countController);
  }

  ValueObservable<int> get count => _countController;
  Sink<void> get increment => _incrementController.sink;

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

2. Provide the bloc by using BlocProvider and access the bloc at subtree:

void main() => runApp(
      // Create and provide the bloc.
      BlocProvider<CounterBloc>(
        creator: (_context, _bag) => CounterBloc(),
        child: App(),
      ),
    );

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Access the bloc with O(1) computation complexity.
    final bloc = BlocProvider.of<CounterBloc>(context);
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: StreamBuilder<int>(
            stream: bloc.count,
            initialData: bloc.count.value,
            builder: (context, snap) => Text(
                  'count: ${snap.data}',
                  style: Theme.of(context).textTheme.title,
                ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.add),
          onPressed: () => bloc.increment.add(null),
        ),
      ),
    );
  }
}
  • Computational complexity of of method, which is used for accessing the bloc is O(1).
  • Provided bloc will be disposed when the inner state is disposed 👍

Examples

Technical explanation

Features and bugs

Please file feature requests and bugs at the issue tracker.

You can’t perform that action at this time.