Skip to content

Instantly share code, notes, and snippets.

@yakubenko
Last active March 9, 2019 14:07
Show Gist options
  • Select an option

  • Save yakubenko/e40c25ec023e0e59048211fa1bbfbfc5 to your computer and use it in GitHub Desktop.

Select an option

Save yakubenko/e40c25ec023e0e59048211fa1bbfbfc5 to your computer and use it in GitHub Desktop.

Revisions

  1. yakubenko revised this gist Mar 9, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions CounterBloc.dart
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,19 @@ class CounterBloc {
    int _counter = 0;
    final _stateController = StreamController<int>();

    // This one goes to the StreamBuilder
    Stream<int> get counterStream => _stateController.stream;

    // This is one of posible modificators
    // I chose String for simplicity. We can pass a param of any type and the check it.
    void modifyCounter(String direction) {
    if (direction == 'up') {
    _counter++;
    } else {
    _counter--;
    }

    // This causes the StreamBuilder to rerender itself
    _stateController.sink.add(_counter);
    }

  2. yakubenko created this gist Mar 9, 2019.
    22 changes: 22 additions & 0 deletions CounterBloc.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    import 'dart:async';

    class CounterBloc {
    int _counter = 0;
    final _stateController = StreamController<int>();

    Stream<int> get counterStream => _stateController.stream;

    void modifyCounter(String direction) {
    if (direction == 'up') {
    _counter++;
    } else {
    _counter--;
    }

    _stateController.sink.add(_counter);
    }

    void dispose() {
    _stateController.close();
    }
    }