Bloc Pattern In Flutter 2 :

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
https://fluttertutorial.in/stateful-widget-stateless-widget-for-flutter, https://fluttertutorial.in/rxdart-in-flutter,
https://fluttertutorial.in/bloc-pattern-in-flutter, https://fluttertutorial.in/stream-controller-in-flutter.
BLoC:
BLoc takes input and output. Input is stream of events. Output is transforms stream of state.
Event:
Event is action for example button click, input field, network call. Events are dispatched and convert states.
BlocBuilder:
State is changes automatically rebuild your widgets. It takes bloc as a parameter and a builder function. For example Inherited widget.
mapEventToState:
Compute the state when an action is dispatched. It is information read synchronously.
BlocProvider.of(context):
Reference the provider from a widge. Child widget gets updated.
dispatch: Emit events that mutate the state.
initialState:
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> { @override int get initialState => 0; @override Stream<int> mapEventToState(int currentState, CounterEvent event) async* { switch (event) { case CounterEvent.increment: yield currentState + 1; break; } } } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: BlocProvider<CounterBloc>( bloc:CounterBloc(), child: MyHomePage() ), ); } } class MyHomePage extends StatelessWidget { @override 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( '${count}', ); } ), 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 www.fluttertutorial.in