google_places_autocomplete 0.1.0
google_places_autocomplete: ^0.1.0 copied to clipboard
Seamlessly integrate Google Places API into your Flutter app with this package. Get location autocomplete, detailed place info, and flexible UI implementation.
Google Places Autocomplete #
Overview #
Google Places Autocomplete is a Flutter package that provides seamless integration with the Google Places API. This package enables developers to implement location search functionality with autocomplete suggestions, detailed place information, and more—all while maintaining complete control over the UI.
Key Features #
- UI-Agnostic Design: Implement your own custom UI while the package handles data retrieval
- Autocomplete Search: Get real-time location suggestions as users type
- Detailed Place Information: Access comprehensive place data including addresses, coordinates, phone numbers, and more
- Cross-Platform Support: Works on Android, iOS, Web, and desktop platforms
- Customizable Filtering: Filter results by country, place type, or language
- Performance Optimized: Built-in debounce functionality to reduce unnecessary API calls
Getting Started #
Prerequisites #
To use this package, you need:
- A Google Cloud Platform project with the Places API enabled
- An API key for accessing the Places API (Get API Key)
Installation #
Add the following to your pubspec.yaml
file:
dependencies:
google_places_autocomplete: ^0.1.0
Then run:
flutter pub get
Usage #
Basic Setup #
Import the package:
import 'package:google_places_autocomplete/google_places_autocomplete.dart';
Initialize the service with required parameters:
final placesService = GooglePlacesAutocomplete(
apiKey: 'YOUR_API_KEY',
predictionsListner: (predictions) {
// Handle the predictions here
setState(() {
_predictions = predictions;
});
},
);
// Always initialize the service before use
placesService.initialize();
Fetching Place Predictions #
To get autocomplete predictions as the user types:
// Call this method when the user enters text in the search field
placesService.getPredictions('user input text');
Fetch Place Details #
Once a user selects a prediction, you can fetch detailed information about the place:
final placeDetails = await placesService.getPredictionDetail('PLACE_ID');
// Access various place details
final address = placeDetails.formattedAddress;
final phoneNumber = placeDetails.formattedPhoneNumber;
final location = placeDetails.geometry?.location;
final latitude = location?.lat;
final longitude = location?.lng;
Advanced Configuration #
The package supports several configuration options:
final placesService = GooglePlacesAutocomplete(
apiKey: 'YOUR_API_KEY',
debounceTime: 300, // Milliseconds to wait before making API call (default: 300)
countries: ['us', 'ca'], // Restrict results to specific countries
primaryTypes: ['restaurant', 'cafe'], // Filter results by place types
language: 'en', // Specify the language for results
predictionsListner: (predictions) {
// Handle predictions
},
loadingListner: (isLoading) {
// Track loading state
},
);
Example #
A complete implementation example showing a search field with autocomplete predictions:
class PlaceSearchScreen extends StatefulWidget {
@override
_PlaceSearchScreenState createState() => _PlaceSearchScreenState();
}
class _PlaceSearchScreenState extends State<PlaceSearchScreen> {
final TextEditingController _searchController = TextEditingController();
final String _apiKey = 'YOUR_API_KEY';
late GooglePlacesAutocomplete _placesService;
List<Prediction> _predictions = [];
bool _isLoading = false;
@override
void initState() {
super.initState();
_placesService = GooglePlacesAutocomplete(
apiKey: _apiKey,
predictionsListner: (predictions) {
setState(() {
_predictions = predictions;
});
},
loadingListner: (isLoading) {
setState(() {
_isLoading = isLoading;
});
},
);
_placesService.initialize();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
TextField(
controller: _searchController,
decoration: InputDecoration(
labelText: 'Search for a place',
suffixIcon: _isLoading
? CircularProgressIndicator()
: Icon(Icons.search),
),
onChanged: (value) {
if (value.isNotEmpty) {
_placesService.getPredictions(value);
} else {
setState(() {
_predictions = [];
});
}
},
),
Expanded(
child: ListView.builder(
itemCount: _predictions.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_predictions[index].description ?? ''),
onTap: () async {
final details = await _placesService.getPredictionDetail(
_predictions[index].placeId ?? '',
);
// Do something with the place details
},
);
},
),
),
],
);
}
}
Model Classes #
The package provides the following model classes:
Prediction #
Represents a place prediction from the Google Places API autocomplete endpoint.
Key properties:
description
: The human-readable name of the placeplaceId
: The ID of the place, which can be used to fetch detailed place informationstructuredFormatting
: Contains the main text and secondary text for the predictiontypes
: The types of the predicted place (e.g., 'restaurant', 'establishment')
PlaceDetails #
Represents detailed information about a place.
Key properties:
name
: The name of the placeformattedAddress
: The complete address of the placeformattedPhoneNumber
: The phone number in international formatnationalPhoneNumber
: The phone number in local formatwebsite
: The website URLrating
: The average rating (out of 5)userRatingsTotal
: Total number of user ratingsgeometry
: Contains location information (latitude and longitude)
Platform Support #
This package works on:
- Android
- iOS
- Web
- macOS
- Windows
- Linux
Troubleshooting #
Common Issues #
-
No predictions returned: Ensure your API key has the Places API enabled and has proper restrictions set.
-
Web platform issues: Make sure your API key is properly configured with allowed referrers/domains.
-
Invalid API key: Check that your API key is correct and properly formatted.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgements #
- Google Places API for providing the location data services