Introduction To Flutter Bloc

  1. Stream<T>
class Bloc {final _controller = StreamController<dynamic>.broadcast();int value = 0;Stream<dynamic> get valueStream => _controller.stream;Function(dynamic) get addEvent => _controller.sink.add;} 
void closeStream() {_controller.close();}
void incrementValue() {value = value + 1;addEvent(value);}void decrementValue() {value = value - 1;addEvent(value);}void incrementValueBy10() {value = value + 10;addEvent(value);}void decrementValueBy10() {value = value - 10;addEvent(value);}void addErrorToStream() {_controller.addError("Streaming error");}void addString() {_controller.add("HELLO WORLD");}
class MyHomePage extends StatefulWidget {@override_MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State<MyHomePage> {Bloc bloc;
@overridevoid initState() {bloc = Bloc();super.initState();}@overridevoid dispose() {bloc.closeStream();super.dispose(); }}
StreamBuilder<dynamic>(initialData: "Getting Started",stream: stream,builder: (context, snapshot) {if (snapshot.hasError) return Text(snapshot.error, style: style);if (snapshot.data == null)return Text("Null",style: style,);if (snapshot.hasData) return Text("${snapshot.data}", style: style2);return Text("Something went wrong", style: style);});
@overrideWidget build(BuildContext context) {print("I am Child Rebuilding ");final style = const TextStyle(backgroundColor: Colors.red, color: Colors.yellow, fontSize: 40);final style2 = const TextStyle(fontSize: 40,fontWeight: FontWeight.bold,backgroundColor: Colors.green,letterSpacing: 2.0);return StreamBuilder<dynamic>( ...........

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Playing with elevation in Android

My project.first.kt #1

Save Data on Android Using Room Database — Beginners Guide

Unit Tests and Concurrency

AutocompletePrediction: using New Places SDK Client

How to Create Spinner — android

Coroutine Task Executor

How I Learned to Stop Worrying and Love the ViewModel

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ankit Gupta

Ankit Gupta

More from Medium

Google Sign-in in Flutter

How to Maintain Browser Data in Flutter Web Between Chrome Runs?

The Flutter app development lifecycle: How to fool-proof it?

Flutter App Development Life Cycle

Developing a new flutter app short tricks for beginners