Skip to main content

Flutter

This SDK supports the following versions:

  • Android version 21 & above
  • iOS version 12 & Above
  • Apple TvOS version 13 & Above
  • Apple WatchOS version 7 & Above

Installation

Add this to your pubspec.yaml file

growthbook_sdk_flutter: ^1.0.0

Quick Usage

To create a GrowthBook SDK instance, use GBSDKBuilderApp. Then, you can evaluate feature flags or run experiments.

// User attributes for targeting and assigning users to experiment variations
val attrs = HashMap<String, Any>()
attrs.put("id", "123")
attrs.put("env", "dev")
attrs.put("betaUser", true)

final GrowthBookSDK sdkInstance = GBSDKBuilderApp(
apiKey: "<API_KEY>",
attributes: {
attrs
},
growthBookTrackingCallBack: (gbExperiment, gbExperimentResult) {},
hostURL: '<GrowthBook_URL>',
).initialize();

if (gb.feature("my-feature").on) {
// Feature is enabled!
}

Using Features

The feature method takes a String feature name and returns a GBFeatureResult object with a few useful properties:

  • value (dynamic) - The assigned value of the feature
  • on (bool) - The value cast to a boolean
  • off (bool) - The value cast to a boolean and then negated
  • source (String) - Why the value was assigned to the user. One of "unknownFeature", "defaultValue", "force", or "experiment"

When the source is "experiment", there are 2 additional properties that tell you which experiment was used and more details about the result of the experiment:

  • experiment (GBExperiment)
  • experimentResult (GBExperimentResult)

Here are some examples:

GBFeatureResult feature = gb.feature("my-feature")

// Do something if feature is truthy
if (feature.on) { }

// Do something if feature is falsy
if (feature.off) { }

// Print the actual value of the feature
// (depending on the feature, might be a string, number, boolean, etc.)
Println(feature.value)

// Print the experiment id used to assign the feature value
if (feature.source == "experiment") {
Println(feature.experiment.key)
}

Running Inline Experiments

Instead of just using features defined in the GrowthBook API, you can also just run an experiment directly. This is done with the run method which takes a GBExperiment object as an argument and returns a GBExperimentResult object:

var exp = GBExperiment()
exp.key = "my-experiment"
exp.variations = List.of("control", "variation")

var result = gb.run(exp)

// Either "control" or "variation"
Println(result.value)

The GBExperiment class has two required properties - key and variations. There are also a number of optional properties:

  • key (String) - The unique identifier for this experiment
  • variations (dynamic[]) - Array of variations to decide between
  • weights (double[]) - How to weight traffic between variations. Must add to 1.
  • active (bool) - If set to false, always return the control (first variation)
  • coverage (double) - What percent of users should be included in the experiment (between 0 and 1, inclusive)
  • condition (GBCondition) - Optional targeting condition
  • namespace ([String, int, int]) - Adds the experiment to a namespace
  • force (int) - All users included in the experiment will be forced into the specific variation index
  • hashAttribute (String) - What user attribute should be used to assign variations (defaults to id)

The GBExperimentResult object returns the following properties:

  • inExperiment (bool)
  • variationId (int) - The array index of the assigned variation
  • value (dynamic) - The value of the assigned variation
  • hashAttribute (String) - The user attribute used to assign a variation
  • hashValue (String) - The value of the attribute used to assign a variation

More Documentation

The GitHub repo for this SDK has more detailed class and method documentation - https://github.com/alippo-com/GrowthBook-SDK-Flutter