title: How can I update a stream from within a StreamBuilder?
body: <p>Thank you very much in advance for your help! Please let me know if the question is not clear i would be happy to add more details if needed.</p>
  <p>I have a Finite State Machine that handles some audio recognition. This FSM is wrapped by a &quot;manager&quot; whose job is to handle the state transitions (processState, nextState). The FSM manager exposes a stream which is updated every time nextState is called</p>
  <p><em><strong>FSM/Manager layout</strong></em></p>
  <pre class="lang-dart prettyprint-override"><code>class FSM_Manager{
  StreamController&lt;RecognitionState&gt; _stateStream =
        StreamController&lt;RecognitionState&gt;();
    Sink&lt;RecognitionState&gt; get _inState =&gt; _stateStream.sink;
    Stream&lt;RecognitionState&gt; get outState =&gt; _stateStream.stream;
    RecognitionState _currentState, _previousState;
   
  void setState(RecognitionState state) {
      _previousState = _currentState;
      _currentState = state;
      _addCurrentStateToStream();
    }
  
    void _addCurrentStateToStream() {
      _inState.add(_currentState);
    }
  
    Future nextState() async {
      _currentState.nextState(this);
    }
  
    Future processState(itemToRecognize) async {
      await _currentState.processState(itemToRecognize);
    }
  }
  
  abstract class BaseState {
    RecognitionStateID get stateID; //enum with each state's ID
    Future processState(itemToRecognize);
    Future nextState(FSM_Manager manager);
  }
  
  class FSM_State1 implements BaseState{
   bool isSuccess = false;
   void processState(itemToRecognize) async {
   isSuccess = await performRecognition(itemToRecognize);  
  }
   void nextState (FSM_Manager fsmManager) {
     if(isSuccess){
       // go to next State
       fsmManager.setState(NEXT_STATE);
     } else {
       //go to some other state
       fsmManager.setState(SOME_OTHER_STATE);
     }
   }
  }
  
  class FSM_State2 implements BaseState{
    bool isSuccess = false;
    void processState(itemToRecognize) async {
      isSuccess = await performRecognition(itemToRecognize); 
   }
   void nextState () {
     if(isSuccess){
       // go to next State
     } else {
       // go to another State
     }
   }
  }
  </code></pre>
  <p>I have a screen (Stateful Widget) which uses a StreamBuidler to listen to the &quot;outState&quot; stream in order to rebuild the screen with the information in the new State.</p>
  <p><em><strong>Stateful Widget</strong></em></p>
  <pre class="lang-dart prettyprint-override"><code>class _RecognitionScreenState extends State&lt;RecognitionScreen&gt; {
    ItemToRecognize item;
    var currStateiD;
    FSM_Manager _fsmManager;
    RecognitionScreenState(
        ItemToRecognize item, FSM_Manager fsmManager) {
      this.item = item;
      this._fsmManager = fsmManager;
    }
  
    @override
    Widget build(BuildContext context) {
      String outString = '';
  
      return StreamBuilder&lt;RecognitionState&gt;(
          stream: _stateContext.outState,
          builder: (BuildContext context,
              AsyncSnapshot&lt;RecognitionState&gt; snapshot) {
            if (snapshot.hasData) {
              outString = snapshot.data.stateID.toString();
  
              return Text(outString);
            } else {
              return Text('');
            }
          });
    }
  }
  </code></pre>
  <p>Now, I do not know where/how to call processState and nextState from, I cant do it from the build method so i Tried to use initState() and didUpdateWidget in the StatefulWidget so that the states are processed in the beginning and after every build respectively. This approach didnt work, the nextState method was never called. I feel like im missing something trivial but i just dont see where to call those functions from outside of the Stateful Widget in order to trigger a rebuild only after the state has changed</p>
  <p>Thanks again for your help</p>
  <p><em><strong>EDIT</strong></em></p>
  <p>I apologize for the confusion,</p>
  <ul>
  <li>I added the BaseState definition (just an abstract class with some method so that i dont forget to implement them)</li>
  <li>the nextState method takes an FSM_Manager as a parameter and calls setState on success or failure and sets the next state</li>
  <li>the States &quot;implement&quot; the BaseState class, they dont &quot;extend&quot; it</li>
  </ul>