local_query_filter 0.1.0
local_query_filter: ^0.1.0 copied to clipboard
A robust, type-safe package for client-side filtering, searching, and sorting of collections in Flutter. Offloads heavy work to a background isolate.
Local Query Filter #
A robust, type-safe, extensible filtering engine for Flutter. Designed for client-side querying of in-memory collections with search, sorting, and pagination, all while maintaining UI performance via isolate-based execution.
๐ Key Features #
- โก High Performance: Filtering is executed in a background isolate via
compute
, preventing UI jank. - ๐งฑ Composable & Type-Safe: Build expressive queries using strongly-typed constraints.
- ๐ Full-Text Search: Perform case-insensitive search across multiple fields.
- ๐ Advanced Sorting: Sort by any
Comparable
field in ascending or descending order. - ๐ Pagination: Supports
limit
andoffset
for effortless paginated lists. - ๐งฉ Extensible by Design: Create your own constraints by extending the
QueryConstraint
class.
๐ Getting Started #
Add the dependency to your pubspec.yaml
:
dependencies:
local_query_filter: ^0.1.0 # Use the latest version
Then run:
flutter pub get
โ๏ธ Usage #
1. Define a Data Model #
class Product {
final String id;
final String name;
final double price;
final List<String> tags;
final DateTime createdAt;
final bool isActive;
final bool isOnSale;
Product({
required this.id,
required this.name,
required this.price,
required this.tags,
required this.createdAt,
required this.isActive,
required this.isOnSale,
});
}
2. Create and Apply a Filter #
final productFilter = QueryFilter<Product>(
constraints: [
// Must be active
BooleanConstraint.isTrue(
fieldExtractor: (product) => product.isActive,
),
// Price under โน100
ComparisonConstraint.lessThan(
value: 100.0,
fieldExtractor: (product) => product.price,
),
// Tag must include 'sale'
ArrayUnionConstraint.arrayContains(
values: ['sale'],
fieldExtractor: (product) => product.tags,
),
// Created in the last 30 days
DateRangeConstraint(
dateRange: DateTimeRange(
start: DateTime.now().subtract(const Duration(days: 30)),
end: DateTime.now(),
),
fieldExtractor: (product) => product.createdAt,
),
],
searchTerm: 'speaker',
searchFieldsExtractor: (product) => [product.name],
sortingFieldExtractor: (product) => product.price,
ascending: true,
limit: 10,
offset: 0,
);
Future<void> runFilter() async {
List<Product> filtered = await productFilter.apply(allProducts);
for (final product in filtered) {
print(product.name);
}
}
๐งช API Overview #
QueryFilter<T>
#
Property | Description |
---|---|
constraints |
List of QueryConstraint s to apply. |
searchTerm |
Case-insensitive search term. |
searchFieldsExtractor |
Extracts fields to apply the search term on. |
sortingFieldExtractor |
Returns the field used for sorting. |
ascending |
Determines sort order. |
limit |
Max number of items to return. |
offset |
Number of items to skip (for pagination). |
QueryConstraint<T>
#
All constraints extend this base class. You can mix and match them freely:
-
โ BooleanConstraint โ Match
true
orfalse
values. -
๐ข ComparisonConstraint โ Operators like
equal
,greaterThan
,lessThan
, etc. -
๐ RangeConstraint โ Check if a value lies within a numeric or comparable range.
-
๐ DateRangeConstraint โ Validate
DateTime
fields fall within a given range. -
๐ท ArrayUnionConstraint โ For list fields. Supports:
arrayContains
arrayContainsAny
whereIn
whereNotIn
-
๐ CompoundConstraint โ Combine multiple constraints using
AND
,OR
, andNOT
. -
๐ง CustomConstraint โ Write any custom boolean function for filtering.
๐ License #
MIT โ see the LICENSE.
๐ค Contributing #
Found a bug? Want to improve performance or add a feature?
Open an issue or PR at: ๐ github.com/shifastudios/local_query_filter