Home > Software engineering >  Firebase Remote Config: How to get all config in Map<String, String>
Firebase Remote Config: How to get all config in Map<String, String>

Time:09-04

Is there a way to get all the configs in String data type? There is a public method getAll() which is a Map<String, FirebaseRemoteConfigValue> but the value is an interface.

@NonNull
  public Map<String, FirebaseRemoteConfigValue> getAll() {
    return getHandler.getAll();
  }

It is guaranteed in our setup that the config values are in String data type.

CodePudding user response:

FirebaseRemoteConfig stores your data as json in your application's files directory. And these values don't have a data type at the moment. You may convert any FirebaseRemoteConfigValue to string but not Boolean or Long.

{
  "configs_key": {
    "hello": "world",
    "flag": "true",
    "isOpen": "true or false :)", // may be string but not bool
  },
  // ...
}

We can see that FirebaseRemoteConfigValue interface always return a string from a value but may throw an IllegalArgumentException for other primitive data types.

interface FirebaseRemoteConfigValue {
  @NonNull String asString();

  boolean asBoolean() throws IllegalArgumentException;
  // ...
}

There may be a tricky solution. If you can add a prefix to your remote config values, you may get them by a specified prefix. This trick requires the developer to set a prefix to all string values. (May not suit to everyone)

// "str_salutation": "hello world", "str_language": "kotlin", "isSomething": "false"
config.getKeysByPrefix("str_").forEach { key ->
  val value = config.getString(key)
  println("key = $key, value=$value")
}

Hope it helps.

CodePudding user response:

So far this is what I ended up, considering that all configuration value for this app is data type of String.

// Convert Firebase Remote Config values to String
 val map = remoteConfig.all.mapValues { it.value.asString() }
  • Related