Flutter: Add Google Maps API key dynamically

Updated 28 April 2023

Save

Instead of hard coding, Google maps API key in AndroidManifest.xml and AppDelegate.swift. We can change the Google Maps key dynamically anytime.

We can store the Google Maps key on the server and can get using the rest API. Map key can change the key at runtime after getting from the server.

Check out more about our Flutter app development.

How to change the google maps API key dynamically?

We have to add a key at run time in native code AndroidManifest.xml and AppDelegate.swift. Flutter provides a way to communicate with native code through the Flutter platform channel.

Flutter Dart Code Implementation :

Let’s check implementation at Fluter’s Dart code end.

Native Android Code Implementation:

Let’s check implementation at Native Android end.

We have to add meta data in AndroidManifest.xml.

Receive Google Maps API key from Flutter Channel.

Native IOS Code Implementation:

Let’s check implementation at Native IOS end.

I hope this blog will help to set Google Map API key dynamically.

Happy Learning 🙂

Reference Links:

https://mobikul.com/flutter-platform-channel/

https://flutter.dev/docs/development/platform-integration/platform-channels

author
. . .

Leave a Comment

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


2 comments

  • fbo
    – The Android part is outdated, please update this (fix is too long to post in comments).
    – Please add the file / location of where to add the native code for Android and iOS (useful for people who do not know the Android / iOS structure):
    Android: ‘MainActivity.kt’ located in ‘/android/app/src/main/kotlin/’
    iOS: ‘AppDelegate.swift’ lcoated in ‘/ios/Runner/’
    – There is a big fault in the iOS code. The FlutterMethodChannel’s name is set to “plugins.flutter.io/firebase_messaging_background”. This should be “com.map_api_key.flutter”.

    Not the best tutorial, not even tested (see last point). But unfortunately the only one that explains both platforms.

    • Christian
      Hi fbo,

      How did you manage to get around it?

      On IOS I managed to make it work.

      But on android it doesn’t seem to update the key.

  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home