match method

Object? match(
  1. T condition, [
  2. String? byDefault = 'Invalid input'
])

The match() function also works similarly to switch

i.e, it finds the matching case according to the condition passed in it.

Map<String, String> map = {
'apple': 'red',
'banana': 'yellow',
'orange': 'orange'
};

print(map.match('apple')); // returns 'red'
print(map.match('pear'));  // returns 'Invalid input'

Implementation

Object? match(T condition, [String? byDefault = 'Invalid input']) =>
    containsKey(condition) ? this[condition] : byDefault;