How to get Google API key in Flutter Application

Issue

I’ve built a Flutter app using https://pub.dev/packages/google_maps_flutter for maps and I want to add directions as per https://medium.com/@shubham.narkhede8/flutter-google-map-with-direction-6a26ad875083, but I need to get the Google API key, which is injected into the app at build time in different ways depending whether it’s Android or IoS.

I’ve spent about an hour googling, but I can’t find a Flutter or native way of getting at the API key in the app. Is there a way?

Solution

If I understand rightly that you’re trying to access the Google Maps API key from your native iOS and Android projects from within Flutter, this should work:

Use this: https://pub.dev/packages/flutter_native_config to read the Android manifest and the iOS Info.plist.

On Android the key will already be there as per the Google Maps docs.

You’ll then need to move your API key in iOS to the info.plist then read it in instead of having it hardcoded: https://www.hackingwithswift.com/example-code/system/how-to-read-your-apps-version-from-your-infoplist-file

Answered By – Tom Alabaster

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *