dazza5000
dazza5000

Reputation: 7618

How to externalize Firebase keys / Google Maps API Keys in a flutter project?

I would like to open source the app I am working on, but still use it personally. How would I externalize the firebase keys and google maps api keys so they aren't available in the public repo?

Upvotes: 2

Views: 1504

Answers (1)

Jacob Phillips
Jacob Phillips

Reputation: 9264

You can have a directory inside lib/ for private code that can go into a separate repo. For private data that is not code, use a top level directory.

Let's say private dirs are called internal. Here is an example tree.

project/
  lib/
    internal/  <-- separate repo.
    view/
    model/
    ...
  test/
  android/
  ios/
  ...

Make sure to add internal/ to .gitignore in your main project. Then make another repo inside the private dir. This is sufficient if the private data doesn't change that often and you don't mind committing the changes separately. There are git managed ways to have nested repos, but it is too difficult for a situation like this imo.

Now just create a class in the internal/ folder and import it from another file just as usual.

internal/keys.dart

final googleMapsKey = 'aj349aj4fp0_394jfam4gni-aoig4ja4goeifnwfa093';
// Firebase key, etc.

view/map.dart

import '../internal/keys.dart';
var googleMap = new GoogleMap(apiKey: googleMapsKey);

You get the idea.

Upvotes: 2

Related Questions