diff options
| author | Rishi-k-s <rishikrishna.sr@gmail.com> | 2025-09-19 16:55:56 +0530 |
|---|---|---|
| committer | Rishi-k-s <rishikrishna.sr@gmail.com> | 2025-09-19 16:55:56 +0530 |
| commit | 2979cf18e1f9036ee092859d224e410026d9b4f5 (patch) | |
| tree | 9da3a2a4409ed6646a4756943d3432c898ad944e | |
| parent | 2651c6833c35e2e3d1b13d67c60de0cbc6d61d44 (diff) | |
fixed messed up api bs
| -rw-r--r-- | lib/main.dart | 40 | ||||
| -rw-r--r-- | lib/utils/debouncer.dart | 18 |
2 files changed, 46 insertions, 12 deletions
diff --git a/lib/main.dart b/lib/main.dart index 3a56154..4c8d838 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,10 @@ +import 'dart:async'; import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'services/places_service.dart'; import 'services/location_service.dart'; import 'models/place_prediction.dart'; +import 'utils/debouncer.dart'; void main() { runApp(const MyApp()); @@ -38,22 +40,35 @@ class _MapSampleState extends State<MapSample> { final PlacesService _placesService = PlacesService(); final LocationService _locationService = LocationService(); + final Debouncer _searchDebouncer = Debouncer(); Future<Iterable<PlacePrediction>> _getLocationSuggestions(String query) async { - if (query.isEmpty) { - return const Iterable<PlacePrediction>.empty(); - } - try { - final predictions = await _placesService.getPlacePredictions( - query, - latitude: _center.latitude, - longitude: _center.longitude, - ); - return predictions; - } catch (e) { - print('Error getting predictions: $e'); + // Return empty list if query is empty or less than 3 characters + if (query.isEmpty || query.length < 3) { return const Iterable<PlacePrediction>.empty(); } + + Completer<Iterable<PlacePrediction>> completer = Completer(); + + _searchDebouncer.call(() async { + try { + final predictions = await _placesService.getPlacePredictions( + query, + latitude: _center.latitude, + longitude: _center.longitude, + ); + if (!completer.isCompleted) { + completer.complete(predictions); + } + } catch (e) { + print('Error getting predictions: $e'); + if (!completer.isCompleted) { + completer.complete(const Iterable<PlacePrediction>.empty()); + } + } + }); + + return completer.future; } @override @@ -75,6 +90,7 @@ class _MapSampleState extends State<MapSample> { void dispose() { _fromController.dispose(); _toController.dispose(); + _searchDebouncer.dispose(); super.dispose(); } diff --git a/lib/utils/debouncer.dart b/lib/utils/debouncer.dart new file mode 100644 index 0000000..a5f2c59 --- /dev/null +++ b/lib/utils/debouncer.dart @@ -0,0 +1,18 @@ +import 'dart:async'; +import 'package:flutter/foundation.dart'; + +class Debouncer { + final Duration delay; + Timer? _timer; + + Debouncer({this.delay = const Duration(milliseconds: 300)}); + + void call(VoidCallback callback) { + _timer?.cancel(); + _timer = Timer(delay, callback); + } + + void dispose() { + _timer?.cancel(); + } +}
\ No newline at end of file |
