Files
posthog.com/contents/docs/integrate/feature-flags-code/_snippets/feature-flags-code-flutter.mdx
2024-12-05 14:09:28 +01:00

36 lines
1.1 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### Boolean feature flags
```dart
if (await Posthog().isFeatureEnabled('flag-key')) {
// Do something differently for this user
// Optional: fetch the payload
final matchedFlagPayload = await Posthog().getFeatureFlagPayload('flag-key');
}
```
### Multivariate feature flags
```dart
if (await Posthog().getFeatureFlag('flag-key') == 'variant-key') { // replace 'variant-key' with the key of your variant
// Do something differently for this user
// Optional: fetch the payload
final matchedFlagPayload = await Posthog().getFeatureFlagPayload('flag-key');
}
```
### Ensuring flags are loaded before usage
Every time a user opens the app, we send a request in the background to fetch the feature flags that apply to that user. We store those flags in the storage.
This means that for most screens, the feature flags are available immediately **except for the first time a user visits**.
### Reloading feature flags
Feature flag values are cached. If something has changed with your user and you'd like to refetch their flag values, call:
```dart
await Posthog().reloadFeatureFlags();
```