- 
      
- 
        Save kwalrath/241c6bc591f9436a9be0116724222953 to your computer and use it in GitHub Desktop. 
    Latest version from https://gist.github.com/miquelbeltran/b4897cc97fec87093139a4aab8228af3
  
        
  
    
      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
    
  
  
    
  | Use the methods `contains` and `startWith` from the `String` class. | 
  
    
      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
    
  
  
    
  | // Implement the predicate of singleWhere | |
| // with the following conditions | |
| // * The element contains the character `'a'` | |
| // * The element starts with the character `'M'` | |
| String singleWhere(Iterable<String> items) { | |
| return items.singleWhere(/* Implement predicate */); | |
| } | 
  
    
      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
    
  
  
    
  | String singleWhere(Iterable<String> items) { | |
| return items.singleWhere((element) => element.startsWith('M') && element.contains('a')); | |
| } | 
  
    
      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 items = [ | |
| 'Salad', | |
| 'Popcorn', | |
| 'Milk', | |
| 'Toast', | |
| 'Sugar', | |
| 'Mozzarella', | |
| 'Tomato', | |
| 'Egg', | |
| 'Water', | |
| ]; | |
| void main() { | |
| try { | |
| final str = singleWhere(items); | |
| if (str == 'Mozzarella') { | |
| _result(true); | |
| } else if (str == null) { | |
| _result(false, [ | |
| 'Tried calling singleWhere, but received a \'null\' value, the result ' | |
| 'should be a non-null String' | |
| ]); | |
| } else { | |
| _result(false, [ | |
| 'Tried calling singleWhere, but received $str instead of the expected ' | |
| 'value \'Mozzarella\'' | |
| ]); | |
| } | |
| } on StateError catch (stateError) { | |
| _result(false, [ | |
| 'Tried calling singleWhere, but received a StateError: ${stateError.message}. ' | |
| 'singleWhere will fail if 0 or many elements match the ' | |
| 'predicate' | |
| ]); | |
| } catch (e) { | |
| _result(false, [ | |
| 'Tried calling singleWhere, but received an exception: $e' | |
| ]); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Updated in: https://gist.github.com/miquelbeltran/b4897cc97fec87093139a4aab8228af3