Created
January 31, 2021 13:58
-
-
Save alphamikle/d74d4db11951ecc425f7176e29ceba0c to your computer and use it in GitHub Desktop.
Search with compute
This file contains 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
Future<void> runSearchWithCompute() async { | |
cacheItems(); | |
isLoading = true; | |
notifyListeners(); | |
searchController.addListener(_searchWithCompute); | |
await _testSearch(); | |
searchController.removeListener(_searchWithCompute); | |
isLoading = false; | |
notifyListeners(); | |
} | |
Future<void> _searchWithCompute() async { | |
canPlaceNextLetter = false; | |
/// Before starting filtering, set a flag that will signal | |
/// that asynchronous filtering is taking place | |
isSearching = true; | |
notifyListeners(); | |
final String searchValue = searchController.text; | |
if (searchValue.isEmpty && items.length != _notFilteredItems.length) { | |
items.clear(); | |
items.addAll(_notFilteredItems); | |
isSearching = false; | |
notifyListeners(); | |
await wait(800); | |
canPlaceNextLetter = true; | |
return; | |
} | |
final List<Item> filteredItems = await compute(filterItems, Packet2(_notFilteredItems, searchValue)); | |
/// After filtering is finished, remove the signal | |
isSearching = false; | |
notifyListeners(); | |
await wait(800); | |
items.clear(); | |
items.addAll(filteredItems); | |
notifyListeners(); | |
canPlaceNextLetter = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment