Flutter SDK Reference

Updated 4 months ago by Archana Singh

This topic explains how to use the Feature Flags (FF) SDK in your Flutter application. To learn more about using a FF SDK with a Flutter application, clone and run a sample application from the Flutter SDK GitHub repository.

In this topic:

Before You Begin

Prerequisites

  1. Create a feature flag in Continuous Features (CF). Feature flags wrap your code and allow you to manage the feature release in a controlled way. See Create a Feature Flag.
  2. Download the Harness Feature Flag Flutter Client SDK.
  3. Ensure that you have created your SDK Key. See Create an SDK Key.
  4. A Flutter application to test your feature flag. If you do not have your Flutter Application, you can download a sample application from the Flutter SDK GitHub repository.

Use Feature Flags SDKs with Flutter Applications

Perform the following steps to use the Feature Flags SDK in your Flutter application:

Step 1: Add the FF Dependency to Your Flutter Application

To start, add the Harness FF Flutter SDK dependency in the pubspec.yaml file.

ff_flutter_client_sdk: ^0.0.1

Step 2: Import the Package to Your Project

Import the following packages to your project in an IDE such as IntelliJ or AndroidStudio.

import 'package:ff_flutter_client_sdk/CfClient.dart';
import 'package:ff_flutter_client_sdk/CfConfiguration.dart';
import 'package:ff_flutter_client_sdk/CfTarget.dart';

Step 3: Authorize your Application to Connect to the FF Client

CfClient is a base class that provides all the features of SDK. The SDK keys authorize your application to connect to the FF client using an SDK key.

final conf = CfConfigurationBuilder()
.setStreamEnabled(true)
.setPollingInterval(60)
.build();
final target = CfTargetBuilder().setIdentifier(name).build();

final res = await CfClient.initialize(apiKey, conf, target);

  • setPollingInterval: Time in seconds (minimum value is 60).
  • apiKey: The SDK keys authorize your application to connect to the CF client. See Step 3: Create an SDK Key.
  • target: Represents the desired target for which the feature needs to be evaluated.

Step 4: Evaluate Target for Your Feature Flag

Once you have initialized the FF client for a target, evaluate it for your feature flag. A feature flag is evaluated for a particular target.

Evaluation is performed based on the variation types. In case there is no evaluation with the provided ID, the default value is returned.

Boolean Variation

//get boolean evaluation
final evaluation = await CfClient.boolVariation("demo_bool_evaluation", false);

Number Variation

//get number evaluation
final numberEvaluation = await CfClient.numberVariation("demo_number_evaluation", 0);

String Variation

//get string evaluaation
final stringEvaluation = await CfClient.stringVariation("demo_string_evaluation", "default");

JSON Variation

//get json evaluation
final jsonEvaluation = await CfClient.jsonVariation("demo_json_evaluation", {});

Step 5: Add Method to Register the Events

This method provides a way to register a listener for different events that might be triggered by SDK, indicating a specific change in the SDK.

CfClient.registerEventsListener((EvaluationResponse, EventType) {

});

The triggered event will have one of the following types.

enum EventType {
SSE_START,
SSE_END,
EVALUATION_POLLING,
EVALUATION_CHANGE
}

Each type will return a corresponding value as shown in the following table:

| EventType          | Returns                  |
| :---------------- | :-----------------------:|
| SSE_START | null |
| SSE_END | null |
| EVALUATION_POLLING | List<EvaluationResponse> |
| EVALUATION_CHANGE | EvaluationResponse |

When the listener is not needed, you can remove the desired listener from the internal list to avoid unexpected behavior.

CfClient.unregisterEventsListener(eventsListener)

When SDK is not needed, for example, when the app is not running, you can shut down the SDK. This can avoid potential memory leaks.

CfClient.destroy()

Step 6: Verify Your Feature Flag

Run the application from your IDE and verify whether the flag variation value displayed on your application page is consistent with the feature flag you created.

Toggle the flag on and off to verify if your application is getting updated.

Public API Methods

You can also use the public API methods to initialize and implement the CF Flutter SDKs. The Public API exposes the following methods that you can utilize:

static Future<InitializationResult> initialize(String apiKey, CfConfiguration configuration, CfTarget target)

static Future<bool> boolVariation(String evaluationId, bool defaultValue)

static Future<String> stringVariation(String evaluationId, String defaultValue)

static Future<double> numberVariation(String evaluationId, double defaultValue)

static Future<Map<dynamic, dynamic>> jsonVariation(String evaluationId, Map<dynamic, dynamic> defaultValue)

static Future<void> registerEventsListener(CfEventsListener listener)

static Future<void> unregisterEventsListener(CfEventsListener listener)

static Future<void> destroy()

Sample FF SDK for Flutter Application

Here is a sample code for using FF SDKs with the Flutter application.

To learn more about using a sample FF SDK code with the Flutter application, see the Flutter SDK GitHub repository.

final conf = CfConfigurationBuilder()
.setStreamEnabled(true)
.setPollingInterval(60) //time in seconds (minimum value is 60)
.build();
final target = CfTargetBuilder().setIdentifier(name).build();

final res = await CfClient.initialize(apiKey, conf, target);


//get number evaluation
final numberEvaluation = await CfClient.numberVariation("demo_number_evaluation", 0);

//get string evaluaation
final stringEvaluation = await CfClient.stringVariation("demo_string_evaluation", "default");

//get json evaluation
final jsonEvaluation = await CfClient.jsonVariation("demo_json_evaluation", {});

CfClient.registerEventsListener((responseData, eventType) {
_eventListener = (responseData, eventType){};
switch (eventType) {
case EventType.SSE_START:
print("Started SSE");
break;
case EventType.SSE_END:
print("SSE Completed");
break;
case EventType.EVALUATION_CHANGE:
String flag = (responseData as EvaluationResponse).flag;
dynamic value = (responseData as EvaluationResponse).value;

break;
case EventType.EVALUATION_POLLING:
List pollingResult = responseData;

pollingResult.forEach((element) {
String flag = (element as EvaluationResponse).flag;
dynamic value = (element as EvaluationResponse).value;

});
break;
}
});

//Shutting down SDK
CfClient.destroy()


Please Provide Feedback