I was struggling implement simple search function using SearchDelegate. I want to wait 2 second after user stop typing and then make a call request to an API.
I already check this question: How to debounce search suggestions in flutter's SearchPage Widget? and use this package https://pub.dev/packages/debounce_throttle but still not working as I expected. Every time user typing a new character, the app immediately make a call request.
This is sample code for buildResults function
@override
Widget buildResults(BuildContext context) {
return FutureBuilder(
builder: (BuildContext context, AsyncSnapshot<List<Place>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return ListView.builder(
itemBuilder: (context, index) => ListTile(
leading: Icon(Icons.location_city),
title: Text(snapshot.data![index].name),
onTap: () { ... do something ... },
),
itemCount: snapshot.data!.length,
);
} else {
return Center(child: CircularProgressIndicator());
}
},
future: queryChanged(query),
);
}
and Future function to make a call request
Future<List<Place>> _getLocation() async {
List<Place> displayPlaces = [];
...
...
return displayPlaces;
}
Debouncer code
final debouncer = Debouncer<String>(Duration(milliseconds: 2000), initialValue: '');
Future<List<Place>> queryChanged(String query) async {
debouncer.value = query;
return _getLocation();
}