Flutter SDK
This Flutter SDK is designed to help you integrate with Unleash and evaluate feature flags inside your application.
You can use this client with Unleash Enterprise or Unleash Open Source.
This is a lightweight Unleash Frontend SDK you can use together with Unleash Frontend API or Unleash Edge. This makes it super simple to use Unleash from any Flutter app.
How to Use the Client as a Module
Step 1: Installation
Step 2: Initialize the SDK
Configure the client according to your needs. The following example provides only the required options. Refer to the section on available options for the full list.
Connection Options
To connect this SDK to your Unleash instance’s front-end API, use the URL to your Unleash instance’s front-end API (<unleash-url>/api/frontend) as the url parameter. For the clientKey parameter, use a FRONTEND token generated from your Unleash instance. Refer to the API tokens documentation for more information.
To connect this SDK to the Unleash Edge, use the URL of Unleash Edge with a Frontend token from an upstream Unleash instance or use a pretrusted token from Unleash Edge.
Step 3: Let the Client Synchronize
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.
Step 4: Check Feature Toggle States
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:
Updating the Unleash Context
The Unleash context is used to evaluate features against attributes of a the current user. 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.
Available Options
The Unleash SDK takes the following options:
Listen for Updates via the Events_emitter
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.
Available 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.SessionId - Important Note
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 toggles is evaluated, in combination with a gradual (percentage based) rollout.
Stop the SDK
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
Now it is possible to bootstrap the SDK with your own feature toggle configuration when you don’t want to make an API call.
This is also useful if you require the toggles to be in a certain state immediately after initializing the SDK.
How to Use It?
Add a bootstrap attribute when 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.
Experimental: Skip Fetching Toggles on Start
If you start multiple clients sharing the same storage provider (e.g. a default SharedPreferencesStorageProvider) you might want to skip fetching toggles on start for all but one of the clients.
This can be achieved by setting the togglesStorageTTL to a non-zero value.
E.g in the example below, the toggles will be considered valid for 60 seconds and will not be fetched on start if they already exist in storage.
We recommend to set togglesStorageTTL to a value greater than the refreshInterval.
Release Guide
You can release manually or use the automated GitHub Action:
Automated Release (Recommended)
- Go to the Actions tab in GitHub and select the Release SDK workflow.
- Click Run workflow and enter the desired version (e.g.
1.9.7). - The workflow will bump the version, update the SDK metadata, prepend the changelog, push a
release/<version>branch, fast-forward merge it tomain, and create/push thev<version>tag. - Publishing to pub.dev happens automatically on tag push via the separate Publish to pub.dev workflow (OIDC). Monitor that workflow for publish status.
Manual Release
- Run tests:
flutter test - Format code:
dart format lib test - Analyse code:
flutter analyze lib test - (optional) run example app (from inside the
exampledir):flutter run - Bump up the version in
pubspec.yaml - Update
CHANGELOG.mdand the SDK version string inlib/unleash_proxy_client_flutter.dart - Create a PR in a new branch, merge it to
main - Create an annotated tag on
main(e.g.v1.9.7) and push it: this triggers the Publish to pub.dev workflow