Last active
          November 22, 2019 15:48 
        
      - 
      
- 
        Save miquelbeltran/f4c780239a1438505ddc7e9404b513ed to your computer and use it in GitHub Desktop. 
    Exercise: Listening to events
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Did you remember to call listen() on the stream? | |
| Did you call to processValue inside listen()? | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import 'dart:async'; | |
| void listenToStream(Stream<String> stream) { | |
| // implement this method | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | void listenToStream(Stream<String> stream) { | |
| stream.listen((event) => processValue(event)); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var processWasCalled = false; | |
| void processValue(String value) { | |
| print(value); | |
| processWasCalled = value == "event"; | |
| } | |
| void main() { | |
| // ignore: close_sinks | |
| final controller = StreamController<String>(sync: true); | |
| final stream = controller.stream; | |
| listenToStream(stream); | |
| // Check if student used `listen` | |
| if (!controller.hasListener) { | |
| _result(false, ['Something went wrong! Stream has no listeners. Did you call to `stream.listen()`?']); | |
| return; | |
| } | |
| controller.add("event"); | |
| // Check if student called to processValue inside listen | |
| if (processWasCalled) { | |
| _result(true); | |
| } else { | |
| _result(false, ['Something whent wrong! `processValue()` was not called when emitting a value']); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment