简体   繁体   中英

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

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.

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.

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() }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM