google_places_autocomplete 0.1.0 copy "google_places_autocomplete: ^0.1.0" to clipboard
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 #

Flutter 3.24.4 pub package License: MIT

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:

  1. A Google Cloud Platform project with the Places API enabled
  2. 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 place
  • placeId: The ID of the place, which can be used to fetch detailed place information
  • structuredFormatting: Contains the main text and secondary text for the prediction
  • types: The types of the predicted place (e.g., 'restaurant', 'establishment')

PlaceDetails #

Represents detailed information about a place.

Key properties:

  • name: The name of the place
  • formattedAddress: The complete address of the place
  • formattedPhoneNumber: The phone number in international format
  • nationalPhoneNumber: The phone number in local format
  • website: The website URL
  • rating: The average rating (out of 5)
  • userRatingsTotal: Total number of user ratings
  • geometry: Contains location information (latitude and longitude)

Platform Support #

This package works on:

  • Android
  • iOS
  • Web
  • macOS
  • Windows
  • Linux

Troubleshooting #

Common Issues #

  1. No predictions returned: Ensure your API key has the Places API enabled and has proper restrictions set.

  2. Web platform issues: Make sure your API key is properly configured with allowed referrers/domains.

  3. 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
6
likes
160
points
342
downloads

Publisher

verified publishercuboidinc.com

Weekly Downloads

Seamlessly integrate Google Places API into your Flutter app with this package. Get location autocomplete, detailed place info, and flexible UI implementation.

Repository (GitHub)

Topics

#network #http #google-places #flexible-ui #map

Documentation

API reference

License

MIT (license)

Dependencies

dio, flutter, rxdart

More

Packages that depend on google_places_autocomplete