Bloc Pattern In Flutter 2

Bloc Pattern In Flutter 2 :

BLoC Pattern Event In Flutter

If you learn BLoC pattern this below point must be learn.
1) Stateless and stateful widget
2) StreamBuilder
3) Stream
4) Observable
5) StreamController
6) Subject, BehaviorSubject, PublishSubjec, ReplaySubject
7) Provider
8) Asyc, wait, function

This link must be learn,,,


BLoc takes input and output. Input is stream of events. Output is transforms stream of state.


Event is action for example button click, input field, network call. Events are dispatched and convert states.


State is changes automatically rebuild your widgets. It takes bloc as a parameter and a builder function. For example Inherited widget.


Compute the state when an action is dispatched. It is information read synchronously.


Reference the provider from a widge. Child widget gets updated.
dispatch: Emit events that mutate the state.


First time call function.

Notes :

  • BLoC is instantiated, make sure that it is a StatefulWidget because properly disposing BLoCs is a must. dispose is clean resource.

Program :

import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

enum CounterEvent { increment } 

class CounterBloc extends Bloc<CounterEvent, int> { 
  int get initialState => 0;

  Stream<int> mapEventToState(int currentState, CounterEvent event) async* {
    switch (event) {
      case CounterEvent.increment:
        yield currentState + 1;

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocProvider<CounterBloc>( 
        child: MyHomePage()

class MyHomePage extends StatelessWidget {
  Widget build(BuildContext context) {

    final CounterBloc _counterBloc = BlocProvider.of<CounterBloc>(context); 

      return Scaffold(
        appBar: AppBar(
          title: Text('BLoC'),
        body: BlocBuilder( 
                bloc: _counterBloc,
                builder: (BuildContext context, int count) {
                  return Text(
        floatingActionButton: FloatingActionButton(
          onPressed: () => _counterBloc.dispatch(CounterEvent.increment), 

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit