applyFilter function
Applies a filter to a value or list of values
Filters values based on include and exclude patterns. All patterns must match for a value to pass the filter.
Parameters
value- Value or list to filterfilter- Filter pattern string (space-separated patterns)
Pattern Syntax
- Include:
pattern- Keep values containing this pattern - Exclude:
!pattern- Remove values containing this pattern - Multiple:
pattern1 pattern2- All patterns must match - Escaped:
\,\;,\&- Literal special characters
Returns
- For lists: Filtered list containing only matching values
- For single values: The value if it matches, null otherwise
- For null input: null
Examples
// Include pattern
applyFilter(['Apple', 'Banana'], 'Apple'); // ['Apple']
// Exclude pattern
applyFilter(['Apple', 'Banana'], '!Apple'); // ['Banana']
// Multiple patterns (AND logic)
applyFilter(['Red Apple', 'Green Apple'], 'Apple Red'); // ['Red Apple']
// Escaped space
applyFilter(['Date Fruit', 'DateTime'], r'Date\ Fruit'); // ['Date Fruit']
// Single value
applyFilter('Apple', 'App'); // 'Apple'
applyFilter('Apple', 'Ban'); // null
Implementation
dynamic applyFilter(dynamic value, String filter) {
final parts = parseFilterPattern(filter);
return applyFilterByList(value, parts);
}