Flutter SDK reference
Read time: 4 minutes
Last edited: Aug 28, 2023
Version 3 of the Flutter SDK introduces optional automatic collection of environment attributes. To learn more about upgrading, read Flutter SDK 2.x to 3.0 migration guide.
Version 2 of the Flutter SDK replaces users with contexts. A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more about upgrading, read Flutter SDK 1.x to 2.0 migration guide and Best practices for upgrading users to contexts.
Code samples on this page are from the three most recent SDK versions where they differ.
Overview
This topic documents how to get started with the Flutter SDK, and links to reference information on all of the supported features.
LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
Resource | Location |
---|---|
SDK API documentation | SDK API docs |
GitHub repository | flutter-client-sdk |
Sample application | Flutter |
Published module | pub.dev |
The LaunchDarkly Flutter SDK is compatible with Android SDK versions 21 and higher and with iOS version 11.0 and higher. The LaunchDarkly Flutter SDK does not support other Flutter platforms.
Getting started
After you complete the Getting started process, follow these instructions to start using the LaunchDarkly SDK in your Flutter mobile application.
First, declare a dependency on the LaunchDarkly Flutter SDK:
launchdarkly_flutter_client_sdk: ^3.0.0
Then, import the package in your application code:
import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart';
The Flutter SDK uses a mobile key. Your environment's mobile key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.
After you install the SDK, initialize the single shared instance of LDClient
. To create a client instance, you need your environment's mobile key. This authorizes your application to connect to a particular environment within LaunchDarkly.
Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.
The following example shows the simplest way to create the client:
LDConfig config = LDConfigBuilder('mobile-key-123abc', AutoEnvAttributes.Enabled).build();LDContextBuilder builder = LDContextBuilder();builder.kind('user', 'user-key-123abc');LDContext context = builder.build();await LDClient.start(config, context);
This method of initializing the client lets you use the SDK as soon as it is ready to return evaluated flags.
To block until the SDK receives the most recent feature flag values, use the LDClient.startFuture
method with await
and an optional timeLimit
:
LDConfig config = LDConfigBuilder('mobile-key-123abc', AutoEnvAttributes.Enabled).build();LDContextBuilder builder = LDContextBuilder();builder.kind('user', 'user-key-123abc');LDContext context = builder.build();await LDClient.start(config, context);await LDClient.startFuture(timeLimit: Duration(seconds: 5));
If you have configured the SDK not to make network requests, or if the device does not have a network connection, LDClient.startFuture
returns a Future
that will complete immediately to avoid blocking the application indefinitely.
You must make feature flags available to mobile SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, the context will receive the fallback value for that flag.
To make a flag available to this SDK, check the SDKs using Mobile key checkbox during flag creation, or on the flag's Settings tab. To make all of a project's flags available to this SDK by default, check the SDKs using Mobile key checkbox in your project Settings.
You can use LDClient
to check which variation a particular context will receive for a feature flag.
Here's how:
bool showFeature = await LDClient.boolVariation(flagKey, false);if (showFeature) {// Application code to show the feature}else {// The code to run if the feature is off}
Shut down the client
Shut down the client when your application terminates. To learn more, read Shutting down.
Data collection
To learn more about data collection within this SDK and implications on submissions to the Apple App Store, read the Apple App Store data collection policy.
Supported features
This SDK supports the following features:
- Anonymous contexts and users
- Automatic environment attributes
- Configuration, including
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Monitoring SDK status
- Offline mode
- Private attributes
- Relay Proxy configuration, using proxy mode
- Sending custom events
- Shutting down
- Subscribing to flag changes
- User and context configuration