Skip to content

Flutter Google Maps - Setup

First post in the series of Flutter and Google Maps. In this post, I will cover how to setup Google Maps and prepare your Flutter project to use gmaps.

Table of contents

Open Table of contents

Google Cloud Platform

Create a project

First, you need to create a project in Google Cloud Platform. Go to Google Cloud Platform and create a new project.

If you already have a project, you can use it, select it from dropdown in the top bar.

Enable Maps API

In the Navigation menu, go to Google Maps Platform.

On the first open you will be proposed to setup Google Maps Platform.

get-started-on-google-maps-platform.png

Copy your API key, you will need it in the next step.

Keep both Enable all Google Maps APIs for this project and Create budget alerts... checked and click on GO TO GOOGLE MAPS PLATFORM button.

Once a Platform setup task is done, you will need to protect your key. For now you can use a simple IP Addresses restriction type with your public or local ip address. You can always change it later.

Now it’s time to enable maps API. Go to Google Maps Platform: APIs & Services and click on ENABLE button for next APIs:

Flutter project

Add dependencies for Flutter

Use flutter pub add command to add dependencies to your project.

Setup Secrets

The main goal is to avoid submitting an API key to the source control and keep it in a safe place.

It will also prepare your project for CI/CD flow, where you can use different keys for different environments.

Setup Secrets for a Flutter project

For Flutter project, we will use flutter_dotenv package to store our API key in a .env file.

1
await dotenv.load(fileName: ".env");
1
final String _apiKey = dotenv.get('GOOGLE_MAPS_API_KEY');

Pretty simple setup for a Flutter project, compared to Native code.

Setup Secrets for Android and iOS

To correctly handle an API key in the Native code, we will need to make a couple of adjustments.

Android

Open article Secrets Gradle plugin.

Follow this article to install and setup com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin in your Android project.

Note: Skip watching the video in this article, it’s not updated and you will need to redo the setup as in the article.

I won’t describe all the parts of the setup, but I will highlight the main steps:

Terminal window
1
propertiesFileName = "secrets.properties"
2
defaultPropertiesFileName = "local.defaults.properties"

Okay, that was a long list of actions, but check the article, it’s very well written, and you should be able to follow it without any issues.

iOS

iOS is a bit different from Android, but it’s still easy to setup.

My steps to setup iOS secrets are:

1
<key>googleMapsApiKey</key> # this value is important, we will access it in the code later
2
<string>$(GOOGLE_MAPS_API_KEY)</string>
1
<key>NSLocationWhenInUseUsageDescription</key>
2
<string>Your current location.</string>
1
GMSServices.provideAPIKey(Bundle.main.object(forInfoDictionaryKey:"googleMapsApiKey") as? String ?? "");

And we are done setting up iOS secrets.

Conclusion

In this post, we have created a new project in Google Cloud Platform, enabled Maps API and setup protected handling of our API key both in the Flutter and Native code.

This setup will help you to avoid publishing your API key to the source control and prepare your project for CI/CD flow.