Flutter SDK Reference
This topic explains how to use the Feature Flags (FF) SDK in your Flutter application.
This topic describes how to use the Harness Feature Flags SDK for your Flutter application.
For getting started quickly, you can use our sample code from the SDK README. You can also clone and run a sample application from the Flutter SDK GitHub Repository.
Before you begin
You should read and understand the following:
Version
Latest SDK version can be found on GitHub Release Page
Flutter and Dart requirements
To use version 2 and greater of this SDK, make sure you:
Install the Flutter SDK, version 2.10.4 or higher.
Use Dart SDK 2.12 and later.
To use version 1 and earlier versions of this SDK, make sure you:
Install the Flutter SDK, version 2.10.4.
Use Dart SDK 2.7 to 2.10
General requirements
(For iOS apps) Install Xcode.
(For Android apps) Install Android Studio, or install the Android SDK for Command-Line Interface (CLI) only.
Create a Flutter application, or clone our sample application.
Create a Feature Flag on the Harness Platform. If you are following along with the SDK README sample code, make sure your flag is called
harnessappdemodarkmode.
Install the SDK for Flutter Web
To install the SDK, you must add the dependency, import the required packages, and then embed the JavaScript SDK.
Add the dependency
Begin by adding the Feature Flag Flutter SDK dependency to your pubspec.yaml file:
Import required packages
Once you've added the dependency, import the necessary packages into your Dart files:
Embed the JavaScript SDK
Be sure that you have added the dependency and imported the required packages before you begin this step.
Embed our JavaScript SDK by adding the following script tag to the
<head>section of your web page:
This installs the Feature Flags JavaScript SDK and makes it available to your application. Please ensure you regularly upgrade the JavaScript SDK version to get the latest updates. For the newest JavaScript SDK updates, monitor:
Release mode for Android applications
In release mode, Flutter applies optimizations that can affect the behavior of native Android code, including code used by our Flutter Android plugin.
Please add the following rule to your ProGuard configuration to ensure proper functionality when running your Android app in release mode
-keep class io.harness.cfsdk.** { *; }
Initialize the SDK
To initialize the Flutter SDK, you need to:
Add a Target that you want to Evaluate against a Feature Flag.
(Optional) Configure the SDK options.
Add your Client SDK key to connect to your Harness Environment.
Complete the initialization with the SDK using the Client SDK Key, Target, and Configuration parameters you set.
Add a Target to Evaluate
For more information about Targets, go to Targeting Users With Flags.
To add a Target, build it and pass in arguments for the following:
Parameter
Description
Required?
Example
identifier
Unique ID for the Target.Read Regex requirements for Target names and identifiers below for accepted characters.
Required
.setIdentifier("HT_1")
name
Name for this Target. This does not have to be unique. Note: If you don’t provide a value, the name will be the same as the identifier.Read Regex requirements for Target names and identifiers below for accepted characters.
OptionalNote: If you don't want to send a name, don't send the parameter. Sending an empty argument will cause an error.
.setName("Harness_Target_1")
For example:
Configure the SDK
You can configure the following options for the SDK:
Name
Configuration Option
Description
Default Value
baseUrl
setConfigUri("https://config.ff.harness.io/api/1.0")
The URL used to fetch Feature Flag Evaluations. When using the Relay Proxy, change this to: http://localhost:7000
https://config.ff.harness.io/api/1.0
eventsUrl
setEventUrl("https://events.ff.harness.io/api/1.0")
The URL for posting metrics data to the Feature Flag service. When using the Relay Proxy, change this to: http://localhost:7000
https://events.ff.harness.io/api/1.0
pollInterval
setPollingInterval(60)
The interval in seconds that we poll for changes when you are using stream mode.
60 (seconds)
enableStream
setStreamEnabled(True)
Set to true to enable streaming mode.Set to false to disable streaming mode.
true
enableAnalytics
setAnalyticsEnabled(True)
Set to true to enable analytics.Set to false to disable analytics.Note: When enabled, analytics data is posted every 60 seconds.
true
For example:
Complete the initialization
CfClient is a base class that provides all the features of the SDK, which can be accessed with CfClient.initialize.
To initialize the SDK, you must pass in the following:
apiKey- The Client SDK Key you created when creating the Feature Flag.Any configuration options you want to use.
The Target you want to evaluate.
Sample of initializing the SDK
Evaluate a Flag
Evaluating a Flag is when the SDK processes all Flag rules and returns the correct Variation of that Flag for the Target you provide.
If a matching Flag can’t be found, or the SDK can’t remotely fetch flags, the default value is returned.
There are different methods for the different Variation types and for each method you need to pass in:
Identifier of the Flag you want to evaluate
The default Variation
The Flag is evaluated against the Target you pass in when initializing the SDK.
Evaluate a boolean Variation
Evaluate a number Variation
Evaluate a string Variation
Evaluate a JSON Variation
Listen for events
Register the events listener
The eventsListener method provides a way to register a listener for different events that might be triggered by SDK.
The possible events and their responses are outlined in the following table:
EventType
Response
SSE_START
null
SSE_END
null
EVALUATION_POLLING
List<EvaluationResponse>
EVALUATION_CHANGE
EvaluationResponse
To listen for events, register the events listener, for example:
The triggered event will return one of the following types:
Close the events listener
To avoid unexpected behavior, when the listener isn't needed, turn it off by calling CfClient.getInstance().unregisterEventsListener(eventsListener), for example:
Test your app is connected to Harness
When you receive a response showing the current status of your Feature Flag, go to the Harness Platform and toggle the Flag on and off. Then, check your app to verify if the Flag Variation displayed is updated with the Variation you toggled.

Close the SDK client
In most applications, you won't need to close the SDK client.
However, you should close the SDK client if:
Your application is about to terminate. Closing the client ensures that all associated resources are released.
You have determined that you do not need to evaluate flags again in your application lifecycle.
To close the SDK client, call this method:
Additional options
Use our public API methods
The Public API exposes the following methods that you can use:
Sample code for a Flutter application
Here is a sample code for using FF SDKs with the Flutter application.
Last updated
Was this helpful?