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>( ...........

--

--

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