Component - BLoC pattern basics
The BLoC (Business Logic Component) pattern helps separate the app's logic from its UI. It uses streams to manage state changes and keeps the UI simple and reactive.
The BLoC (Business Logic Component) pattern helps separate the app's logic from its UI. It uses streams to manage state changes and keeps the UI simple and reactive.
Scaffold
├─ AppBar
│ └─ Text
└─ Center
└─ Column
├─ StreamBuilder
│ └─ Text
└─ ElevatedButtonBLoC stands for Business Logic Component.final _controller = StreamController<int>();
void addNumber(int n) {
_controller.sink.add(n);
}
Stream<int> get stream => _controller.stream;
// In UI:
StreamBuilder<int>(
stream: bloc.stream,
builder: (context, snapshot) {
return Text(snapshot.data?.toString() ?? 'No data');
},
);addNumber(5)?class CounterBloc {
final _controller = StreamController<int>();
void increment() {
_controller.sink.add(_counter + 1);
}
Stream<int> get stream => _controller.stream;
}_counter is not declared anywhere, what is the issue?Pass the test and this pattern moves unknown