Reverse geocoding in Flutter for MacOS

For many years, I have been using https://pub.dev/packages/geocoding successfully, but recently I've had to migrate away from it.

This package is great because it is compatible with Android and iOS, and it uses the device's built-in reverse geocoding feature. Since it's built into the device, I don't need an API key to hit the API.

However, I want to run my app as a MacOS app in MacOS and geocoding does not support it.

Someone filed a ticket to the repository back in 2002 to ask for MacOS support, and someone even proposed a pull request around the same time, but despite the package releasing updates regularly, it has been completely ignored.

Instead I'll use the Geocoding API on Google Cloud.

Enabling the API

Go to the Google Cloud Console, APIs, look for Maps Platform APIs. Then in credentials, you can create some API keys. https://console.cloud.google.com/google/maps-apis/credentials. The API to call looks like this:

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
Reverse geocoding (address lookup) request and response | Geocoding API | Google for Developers

googleapis

I thought I could use the googleapis package, which is an official package auto-generated from Google API protobufs.

googleapis | Dart package
Auto-generated client libraries for accessing Google APIs described through the API discovery service.

However, to my surprise, the geocoding API is not in there!

googleapis - Dart API docs
googleapis API docs, for the Dart programming language.

That means I should either find a non-official package or use the REST API manually.

Non-official package

google_geocoding_api | Dart package
This Package implement Google Geocoding API with default and reverse geosearch

Luckily someone already made a package that uses the REST API. They use dio to make the GET requests. https://github.com/Dimolll/google_geocoding_api/blob/e48620092049eddc558c38c1d78921985fa3829e/lib/src/api/geocoding_api.dart#L79

dio | Dart package
A powerful HTTP networking package,supports Interceptors,Aborting and canceling a request,Custom adapters, Transformers, etc.

I followed the README and it worked perfectly.