google_places_autocomplete 0.0.3 google_places_autocomplete: ^0.0.3 copied to clipboard
A Flutter package for integrating Google Places(new) API into your app. This package enables autocomplete suggestions, detailed place information retrieval, and more. It simplifies working with the Go [...]
Google Places Autocomplete #
Google Places Autocomplete is a Flutter package for integrating Google Places(new) API into your app. This package enables autocomplete suggestions, detailed place information retrieval, and more. It simplifies working with the Google Places API in Flutter projects. #
Google Places Autocomplete is a comprehensive Flutter package that facilitates seamless integration of the new Google Places API into your app. It empowers developers to provide rich, location-based features such as autocomplete suggestions, detailed place information retrieval, and interactive maps.
With this package, users can easily search for locations using Google’s powerful Places Autocomplete API. The package supports advanced features like filtering results by country, type, or language, and includes debounce functionality to ensure a smooth user experience by reducing unnecessary API calls. Additionally, it enables fetching detailed place information, including addresses, geographical coordinates, phone numbers, and more.
Designed with developers in mind, the package simplifies complex interactions with the Google Places API through a clean, modular structure. It integrates seamlessly with Flutter UI components, and works well alongside google_maps_flutter
for displaying maps and location data. Whether you’re building a travel app, a delivery service, or any location-aware app, this package offers a robust, flexible solution to meet your needs.
With detailed documentation, examples, and customization options, Google Places Autocomplete provides everything you need to deliver accurate, location-based functionality in your Flutter applications.
Features #
- Autocomplete search with suggestions and predictions.
- Fetch detailed place information (address, phone numbers, coordinates, etc.).
- Support for filtering by country, place type, and language.
- Debounce functionality to improve performance and UX.
- Integration with
google_maps_flutter
for seamless map interactions.
Getting Started #
Prerequisites #
To use this package, you need:
- A Google Cloud Platform project with the Places API(new) enabled.
- An API key for accessing the Places API. You can obtain an API key by following the Google API Documentation.
Installation #
Add the following to your pubspec.yaml
file:
dependencies:
google_places_autocomplete: latest_version
Then, run:
flutter pub get
Usage #
Basic Setup #
- Import the package:
import 'package:google_places_autocomplete/google_places_autocomplete.dart';
- Initialize the service:
final googlePlaces = GooglePlacesAutocomplete(
apiKey: "YOUR_API_KEY",
predictionsListner: (predictions) {
// Handle predictions
},
debounceTime: 300, // Customize debounce time
countries: ['us'], // Restrict to the US
primaryTypes: ['locality'], // Focus on specific types
);
googlePlaces.initialize();
- Get predictions:
googlePlaces.getPredictions("San Francisco");
or you can pass it directly to onChanged parameter of TextFields
TextFormField(
controller: textEditingController,
focusNode: focusNode,
onChanged:googlePlaces.getPredictions,
)
- Get place details:
final placeDetails = await googlePlaces.getPredictionDetail("PLACE_ID");
print(placeDetails?.formattedAddress);
Example #
Below is an example of integrating Google Places Autocomplete with a Flutter TextField widget:
import 'package:flutter/material.dart';
import 'package:google_places_autocomplete/google_places_autocomplete.dart';
class PlaceSearchScreen extends StatefulWidget {
@override
_PlaceSearchScreenState createState() => _PlaceSearchScreenState();
}
class _PlaceSearchScreenState extends State<PlaceSearchScreen> {
late final GooglePlacesAutocomplete _placesAutocomplete;
@override
void initState() {
super.initState();
_placesAutocomplete = GooglePlacesAutocomplete(
apiKey: "YOUR_API_KEY",
predictionsListner: (predictions) {
// Handle predictions and update UI
},
);
_placesAutocomplete.initialize();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Search Places")),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
decoration: InputDecoration(labelText: "Search Location"),
onChanged: (value) => _placesAutocomplete.getPredictions(value),
),
// Add widgets to display predictions and handle interactions
],
),
),
);
}
@override
void dispose() {
_placesAutocomplete.dispose();
super.dispose();
}
}
API Reference #
GooglePlacesAutocomplete Constructor #
GooglePlacesAutocomplete({
required String apiKey,
required ListenerAutoCompletePredictions predictionsListner,
int debounceTime = 300,
List<String>? countries,
List<String>? primaryTypes,
String? language,
});
Support #
Feel free to file issues on the GitHub repository for:
- Adding support for additional email apps.
- Reporting bugs or suggesting improvements.
Contributors #
Jamshid Ali 💻 |
Mubashir Saeed 💻 |
License #
This package is distributed under the MIT License. See the LICENSE file for details.