Not disposing the stream , but resetting the counter stream value to 0 and listening to it again

青春壹個敷衍的年華 提交于 2020-05-17 02:59:40

问题


I' m having a Counter Bloc and showing the counter on second page , I want to reset the counter value to 0(ZERO) on navigating back to first page or on pressing back button without disposing the stream so that i can listen to stream throughout the app.

In short whenever returning to second page the stream count should start from 0.

CounterBloc.dart

import 'dart:async';

class CounterBloc {
  int _counter = 0;
  StreamController<int> _countController = StreamController<int>.broadcast();

  Stream<int> get counterStream => _countController.stream;
  StreamSink<int> get counterSink => _countController.sink;

  void increamentCounter() {
    _counter++;
    counterSink.add(_counter);
  }
}

回答1:


You need to create a new method (action) inside your Bloc.

  void resetCounter() {
    _counter = 0;
    counterSink.add(_counter);
  }


来源:https://stackoverflow.com/questions/61694115/not-disposing-the-stream-but-resetting-the-counter-stream-value-to-0-and-liste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!