Skip to content

Instantly share code, notes, and snippets.

@GIfatahTH
Last active April 3, 2020 07:01
Show Gist options
  • Save GIfatahTH/8c56ddb9321659f40218c04751dfa1f5 to your computer and use it in GitHub Desktop.
Save GIfatahTH/8c56ddb9321659f40218c04751dfa1f5 to your computer and use it in GitHub Desktop.
MainBloc class of Bloc Pattern
class MainBloc {
final itemProvider = ItemProvider();
final itemsConroller = StreamController<List<Item>>();
final detailedColorConroller = StreamController<Color>();
final detailedItemConroller = StreamController<Item>();
Stream get itemsStream => itemsConroller.stream;
Stream get detailedColorStream => detailedColorConroller.stream;
Stream get detailedItemStream => detailedItemConroller.stream;
StreamSink<List<Item>> get itemsSink => itemsConroller.sink;
StreamSink<Color> get detailedColorSink => detailedColorConroller.sink;
StreamSink<Item> get detailedItemSink => detailedItemConroller.sink;
List<Item> _items;
int _detailedIndex;
getItems() async {
_items = await itemProvider.fetchItems();
itemsSink.add(_items);
}
showDetailed(Color color, int index, Item item) {
detailedColorSink.add(color);
detailedItemSink.add(item);
_detailedIndex = index;
}
increment() {
_items[_detailedIndex].count++;
itemsSink.add(_items);
detailedItemSink.add(_items[_detailedIndex]);
}
void dispose() {
itemsConroller.close();
detailedColorConroller.close();
detailedItemConroller.close();
}
}
MainBloc mainBloc = MainBloc();
@Allan-Nava
Copy link

Have u got a full example of app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment