[SOLVED] Firebase Remote Config: How to get all config in Map – Stack Overflow

Issue

This Content is from Stack Overflow. Question asked by Bitwise DEVS

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.



Solution

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.


This Question was asked in StackOverflow by Bitwise DEVS and Answered by ocos It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?