getKeyAndValue<T> method
Gets the key of a setting and its value identified by the given variationId
(analytics).
variationId
the Variation ID.
T
the type of the desired feature flag or setting. Only the following types are allowed: String, bool, int, double, Object (both nullable and non-nullable) and dynamic
.
Implementation
Future<MapEntry<String, T>?> getKeyAndValue<T>(
{required String variationId}) async {
_validateReturnType<T>();
try {
final result = await _getSettings();
if (result.isEmpty) {
_errorReporter.error(
1000, 'Config JSON is not present. Returning null.');
return null;
}
for (final entry in result.settings.entries) {
if (entry.value.variationId == variationId) {
return MapEntry(
entry.key,
_parseSettingValue<T>(
entry.value.settingValue, entry.value.type));
}
for (final targetingRule in entry.value.targetingRules) {
if (targetingRule.servedValue != null) {
if (targetingRule.servedValue?.variationId == variationId) {
return MapEntry(
entry.key,
_parseSettingValue<T>(targetingRule.servedValue!.settingValue,
entry.value.type));
}
} else {
var targetRulePercentageOptions = targetingRule.percentageOptions;
if (targetRulePercentageOptions != null) {
for (final percentageOption in targetRulePercentageOptions) {
if (percentageOption.variationId == variationId) {
return MapEntry(
entry.key,
_parseSettingValue<T>(
percentageOption.settingValue, entry.value.type));
}
}
} else {
throw UnsupportedError(
"Targeting rule THEN part is missing or invalid.");
}
}
}
for (final percentageOption in entry.value.percentageOptions) {
if (percentageOption.variationId == variationId) {
return MapEntry(
entry.key,
_parseSettingValue<T>(
percentageOption.settingValue, entry.value.type));
}
}
}
_errorReporter.error(2011,
'Could not find the setting for the specified variation ID: \'$variationId\'.');
return null;
} catch (e, s) {
_errorReporter.error(
1002,
'Error occurred in the `getKeyAndValue` method. Returning null.',
e,
s);
return null;
}
}