This document was generated from README.md in the Flutter GitHub repository.
To connect to Unleash from a frontend application, you'll need to use the Unleash front-end API (how do I create an API token?) or the Unleash proxy (how do I create client keys?).
Unleash SDK for Flutter (Dart)
Unleash is a private, secure, and scalable feature management platform built to reduce the risk of releasing new features and accelerate software development. 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
flutter pub add unleash_proxy_client_flutter
Step 2: Initialize the SDK
💡 TIP: As a Frontend SDK, this SDK requires you to connect to either Unleash Edge or to Unleash Frontend API. Refer to the connection options section for more information.
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.
import 'package:unleash_proxy_client_flutter/unleash_proxy_client_flutter.dart';
final unleash = UnleashClient(
url: Uri.parse('https://<your-unleash-instance>/api/frontend'),
clientKey: '<your-client-side-token>',
appName: 'my-app');
unleash.start();
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 how to create API tokens guide for the necessary steps.
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.
unleash.on('ready', (_) {
if (unleash.isEnabled('flutter.demo')) {
print('flutter.demo is enabled');
} else {
print('flutter.demo is disabled');
}
});
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:
unleash.isEnabled('flutter.demo');
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
final variant = unleash.getVariant('flutter.demo');
if (variant.name == 'blue') {
// something with variant blue...
}
You can also access the payload associated with the variant:
final variant = unleash.getVariant('flutter.demo');
final payload = variant.payload;
if (payload != null) {
// do something with the payload
// print(payload "${payload.type} ${payload.value}");
}
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.
// Used to set the context fields, shared with the Unleash Edge. This
// method will replace the entire (mutable part) of the Unleash Context.
unleash.updateContext(UnleashContext(userId: '1233'));
// Used to update a single field on the Unleash Context.
unleash.setContextField('userId', '4141');
// Used to update multiple context fields on the Unleash Context.
unleash.setContextFields({'userId': '4141'});