In my app I am searching for results as user types something in the a TextField. I'm using Provider where there is a searchProduct() function which is fired every time user types something in the textfield. once the result is fetched I'm calling the notifyListener() function and UI gets updated accordingly.
The problem I'm facing is as the results are being fetched asynchronously, they are not arriving at the same time. Sometimes the last result comes before one of the previous results. This particularly happens when user types in too fast. So with every key stroke this searchProduct() function is being called and it makes network request. This approach is also making too many unnecessary network request, which is not ideal. What would be the best way to solve this issue so that in a given time when user types a search string the search will begin after user is done typing?
class ProductService extends ChangeNotifier {
String _searchText;
String serverUrl = 'https://api.example.com/api';
String get searchText => _searchText;
List<Product> products = [];
bool searching = false;
void searchProduct(String text) async {
searching = true;
notifyListeners();
_searchText = text;
var result = await http
.get("$serverUrl/product/search?name=$_searchText");
if (_searchText.isEmpty) {
products = [];
notifyListeners();
} else {
var jsonData = json.decode(result.body);
List<Map<String, dynamic>> productsJson =
List.from(jsonData['result'], growable: true);
if (productsJson.length == 0) {
products = [];
notifyListeners();
} else {
products = productsJson
.map((Map<String, dynamic> p) => Product.fromJson(p))
.toList();
}
searching = false;
notifyListeners();
}
}
}