Google Places API with Flutter

Fredrick Mbugua
2 min readFeb 17, 2023

Image by pch.vector on Freepik

Google Places API is a powerful tool that allows developers to integrate location-based features into their applications. With Flutter, developers can easily implement this functionality and create engaging, location-based applications.

To start using the Google Places API, you will need to create a Google Cloud Platform account and enable the API. Once you have done that, you will be able to generate an API key, which you will use to access the API from your Flutter application.

Here are the steps to integrate the Google Places API with Flutter:

  1. Add the Google Maps SDK and the Google Places API to your Flutter project by adding the following lines to your pubspec.yaml file:
dependencies:
google_maps_flutter: ^2.1.0
google_maps_webservice: ^0.0.19+1

2. Import the necessary packages in your Dart file:

import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_webservice/places.dart';

3. Create a new instance of the Google Maps API client using your API key:

final _places = GoogleMapsPlaces(apiKey: 'YOUR_API_KEY_HERE');

4. Use the Google Places API client to search for places by providing a search query and location:

Future<List<PlacesSearchResult>> searchPlaces(String query, LatLng location) async {
final result = await _places.searchNearbyWithRadius(
Location(lat: location.latitude, lng: location.longitude),
5000,
type: "restaurant",
keyword: query,
);
if (result.status == "OK") {
return result.results;
} else {
throw Exception(result.errorMessage);
}
}

In this example, we’re searching for nearby restaurants within a 5km radius of a given location. The search query is provided as a keyword argument. The search result is returned as a list of PlacesSearchResult objects.

5. Display the search results on a map or in a list:

ListView.builder(
itemCount: _placesList.length,
itemBuilder: (context, index) {
final place = _placesList[index];
return ListTile(
title: Text(place.name),
subtitle: Text(place.vicinity),
trailing: IconButton(
icon: Icon(Icons.arrow_forward),
onPressed: () => openDetailsScreen(context, place),
),
);
},
);

In this example, we’re displaying the search results in a list view, with each result as a ListTile. When the user taps on a result, we open a details screen with more information about the selected place.

That’s it! With these steps, you can integrate the Google Places API into your Flutter application and add powerful location-based features to your app.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Fredrick Mbugua
Fredrick Mbugua

Written by Fredrick Mbugua

Software Developer @Safaricom. Experienced in Web, Mobile (Flutter & Kotlin), CrytpoCurrency, Smart Contracts, API and Software Design, Interaction Design

Responses (2)

Write a response

This article exposes the API key in the production version, which may cause the API to be stolen and used outside of the app

--

This stoped working in laters flutter version

--