getFlag method

  1. @override
Future<GetFlag> getFlag({
  1. required String featureKey,
  2. required VWOUserContext userContext,
})
override

Gets the value of a feature flag.

featureKey The name of the feature flag. userContext The user context for evaluating the flag.

Returns a Future that resolves to a GetFlag object containing the flag value and other metadata.

Implementation

@override
Future<GetFlag> getFlag({
  required String featureKey,
  required VWOUserContext userContext,
}) async {
  try {
    final dynamic result = await methodChannel.invokeMethod('getFlag', {
      'flagName': featureKey,
      'userContext': userContext.toMap(),
    });

    // Explicitly cast the result to Map<String, dynamic> if possible
    if (result is Map) {
      // We need to cast every entry in the map to ensure the correct types
      final Map<String, dynamic> resultMap = {};
      result.forEach((key, value) {
        if (key is String) {
          resultMap[key] = value; // Ensure value is dynamic
        }
      });
      return GetFlag.fromMap(resultMap);
    } else {
      throw Exception(
          "Expected result type Map<String, dynamic> but got ${result.runtimeType}");
    }
  } catch (e) {
    return Future.error(e);
  }
}