loadFromAsset method

Future<NUIGlobalConfig?> loadFromAsset(
  1. String name
)

Load a specific asset

Implementation

Future<NUIGlobalConfig?> loadFromAsset(String name) async {

  if (!name.endsWith(".json")) {
    name = "$name.json";
  }

  /**
   * Load properties from assets/build
   */
  try {
    logNUI(_MODULE, "Loading configs from file : $name");
    String content = await rootBundle.loadString("assets/build/$name");
    if(isNullOrEmpty(content)){
      throwNUI(_MODULE, "Config file is empty for ${"assets/build/$name"}");
    }
    Map<String, dynamic> configAsMap = json.decode(content);
    logNUI(_MODULE, "Content of config from file : $content");
    _appConfig.addAll(configAsMap);
    return this;
  }catch(e){
    logNUI(_MODULE, "No config file specified for $name with error : $e");
  }

  /**
   * If not found from assets/build,
   * Load properties from assets/env
   */
  try {
    logNUI(_MODULE, "Loading configs from env file : $name");
    String content = await rootBundle.loadString("assets/env/$name");
    if(isNullOrEmpty(content)){
      throwNUI(_MODULE, "Config file is empty for ${"assets/env/$name"}");
    }
    Map<String, dynamic> configAsMap = json.decode(content);
    logNUI(_MODULE, "Content of config from env file : $content");
    _appConfig.addAll(configAsMap);
    return this;
  }catch(e){
    logNUI(_MODULE, "No config file specified for $name with error : $e");
  }

  /**
   * If not found from both assets/build and assets/env
   * Load properties using the direct asset name provided
   */
  try {
    logNUI(_MODULE, "Loading configs from raw file : $name");
    String content = await rootBundle.loadString(name);
    if(isNullOrEmpty(content)){
      throwNUI(_MODULE, "Raw config file is empty for $name");
    }
    Map<String, dynamic> configAsMap = json.decode(content);
    logNUI(_MODULE, "Content of config from raw file : $content");
    _appConfig.addAll(configAsMap);
    return this;
  }catch(e){
    logNUI(_MODULE, "No raw config file specified for $name with error : $e");
  }

  return null;
}