Flutter SDK
The Unleash Flutter SDK lets you evaluate feature flags in Flutter applications. It connects to Unleash or Unleash Edge to fetch evaluated flags for a given Unleash context.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
Installation
Initialization
Configure the client according to your needs. The following example provides only the required options. Refer to configuration options for the full list.
Set url to the /api/frontend endpoint of your Unleash instance or your Unleash Edge instance. Set clientKey to a frontend API token.
Check if the SDK is ready
You should wait for the client’s ready or initialized events before you start working with it. Before it’s ready, the client might not report the correct state for your features.
The difference between the events is explained below.
Check flags
Once the client is ready, you can start checking features in your application. Use the isEnabled method to check the state of any feature you want:
You can use the getVariant method to get the variant of an enabled feature that has variants. If the feature is disabled or if it has no variants, then you will get back the disabled variant
You can also access the payload associated with the variant:
Unleash context
The Unleash context holds properties such as user ID, session ID, and custom fields that drive flag evaluation, including strategy targeting, constraints, and stickiness. To update and configure the Unleash context in this SDK, use the updateContext, setContextField, and setContextFields methods.
The context you set in your app will be passed along to Unleash Edge or the Frontend API as query parameters for feature evaluation.
The updateContext method will replace the entire
(mutable part) of the Unleash context with the data that you pass in.
The setContextField method only acts on the property that you choose. It does not affect any other properties of the Unleash context.
The setContextFields method only acts on the properties that you choose. It does not affect any other properties of the Unleash context.
Configuration options
The Unleash SDK takes the following options:
Events
The client is also an event emitter. This means that your code can subscribe to updates from the client. This is a neat way to update your app when toggle state updates.
The SDK emits the following events:
- error - emitted when an error occurs on init, or when fetch function fails, or when fetch receives a non-ok response object. The error object is sent as payload.
- initialized - emitted after the SDK has read local cached data in the storageProvider.
- ready - emitted after the SDK has successfully started and performed the initial fetch towards Unleash.
- update - emitted every time the Unleash returns a new feature toggle configuration. The SDK will emit this event as part of the initial fetch from the SDK.
unleash.start(). If you register them after you have started the SDK you risk losing important events.Session ID
You may provide a custom session id via the “context”. If you do not provide a sessionId this SDK will create a random session id, which will also be stored in the provided storage. By always having a consistent sessionId available ensures that even “anonymous” users will get a consistent experience when feature flags are evaluated, in combination with a gradual (percentage based) rollout.
Stop the client
You can stop the Unleash client by calling the stop method. Once the client has been stopped, it will no longer check for updates or send metrics to the server.
A stopped client can be restarted.
Bootstrap
You can bootstrap the SDK with your own flag configuration to avoid an API call on startup. Add a bootstrap attribute when you create a new UnleashClient.
There’s also a bootstrapOverride attribute which by default is true.
- If
bootstrapOverrideistrue(by default), any local cached data will be overridden with the bootstrap specified. - If
bootstrapOverrideisfalseany local cached data will not be overridden unless the local cache is empty.
Manage your own refresh mechanism
You can opt out of the Unleash feature flag auto-refresh mechanism and metrics update by settings the refreshInterval and/or metricsInterval options to 0.
In this case, it becomes your responsibility to call updateToggles and/or sendMetrics methods.
Skip fetching flags on start
If you run multiple clients that share the same storage provider (for example, the default SharedPreferencesStorageProvider), you can skip fetching flags on start for all but one client. Set togglesStorageTTL to a non-zero value to reuse cached flags that are still within the TTL window.
In the following example, flags in storage are considered valid for 60 seconds and are not fetched on start if they already exist. Set togglesStorageTTL to a value greater than refreshInterval.