|
| 1 | +/// Represents the result of a feature flag evaluation. |
| 2 | +/// |
| 3 | +/// Contains the flag key, whether it's enabled, the variant (for multivariate flags), |
| 4 | +/// and any associated payload. |
| 5 | +class PostHogFeatureFlagResult { |
| 6 | + /// The feature flag key. |
| 7 | + final String key; |
| 8 | + |
| 9 | + /// Whether the flag is enabled. |
| 10 | + /// |
| 11 | + /// For boolean flags, this is the flag value. |
| 12 | + /// For multivariate flags, this is true when the flag evaluates to any variant. |
| 13 | + final bool enabled; |
| 14 | + |
| 15 | + /// The variant key for multivariate flags, or null for boolean flags. |
| 16 | + final String? variant; |
| 17 | + |
| 18 | + /// The JSON payload associated with the flag, if any. |
| 19 | + final Object? payload; |
| 20 | + |
| 21 | + const PostHogFeatureFlagResult({ |
| 22 | + required this.key, |
| 23 | + required this.enabled, |
| 24 | + this.variant, |
| 25 | + this.payload, |
| 26 | + }); |
| 27 | + |
| 28 | + @override |
| 29 | + String toString() { |
| 30 | + return 'PostHogFeatureFlagResult(key: $key, enabled: $enabled, variant: $variant, payload: $payload)'; |
| 31 | + } |
| 32 | + |
| 33 | + @override |
| 34 | + bool operator ==(Object other) { |
| 35 | + if (identical(this, other)) return true; |
| 36 | + return other is PostHogFeatureFlagResult && |
| 37 | + other.key == key && |
| 38 | + other.enabled == enabled && |
| 39 | + other.variant == variant; |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + int get hashCode => Object.hash(key, enabled, variant); |
| 44 | + |
| 45 | + /// Creates a [PostHogFeatureFlagResult] from a native SDK response map. |
| 46 | + /// |
| 47 | + /// The [map] should contain: key, enabled, variant, payload. |
| 48 | + /// Falls back to [fallbackKey] if the map doesn't include a key. |
| 49 | + /// Returns null if [result] is null or not a Map. |
| 50 | + static PostHogFeatureFlagResult? fromMap(Object? result, String fallbackKey) { |
| 51 | + if (result == null) return null; |
| 52 | + if (result is! Map) return null; |
| 53 | + |
| 54 | + return PostHogFeatureFlagResult( |
| 55 | + key: result['key'] as String? ?? fallbackKey, |
| 56 | + enabled: result['enabled'] as bool? ?? false, |
| 57 | + variant: result['variant'] as String?, |
| 58 | + payload: result['payload'], |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
0 commit comments