Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Decide methods for the Flutter SDK

Overview of the decide methods which can be used to return a flag decision for a user in Optimizely Feature Experimentation.

Use the decide methods to return feature flag decisions for a user. Each decision includes whether the flag is enabled and which variation the user receives.

Decide

Returns a decision for a specific feature flag and user.

Minimum SDK version – v1.0.1+

Description

The decide method handles the variation decision for an individual experiment. It returns a decision result for a flag key for a user in an OptimizelyDecision object and contains the data required to deliver the flag rule.

This method ensures that Feature Experimentation properly buckets the user in experiment analytics, and you should use it if there is no need for pre-rendering decisions.

📘

Note

Decide is a method of the UserContext object. See OptimizelyUserContext for details.

See the OptimizelyDecision documentation for details of the returned decision object.

Parameters

The following table describes parameters for the decide method:

ParameterTypeDescription
keyStringThe key of the feature flag.
options (optional)SetSet of OptimizelyDecideOption enums.

Returns

The decide method returns an OptimizelyDecision object. See OptimizelyDecision.

If the method encounters a critical error (SDK not ready, invalid flag key, and so on), it returns a decision with a null variationKey field and populates the reasons field with error messages (regardless of the OptimizelyDecideOption.INCLUDE_REASONS option).

Example decide

Map<String, dynamic> attributes = {};
attributes["logged_in"] = true;
var user = await flutterSDK.createUserContext(userId: "user123", "3", attributes: attributes);

var decideResponse = await user!.decide("product_sort");

// variation. if null, decision fail with a critical error
var variationKey = decideResponse.decision!.variationKey;

// flag enabled state:
var enabled = decideResponse.decision!.enabled;

// all variable values
var variables = decideResponse.decision!.variables;

// String variable value
var vs = variables["sort_method"] as String;

// Boolean variable value
var vb = variables["k_boolean"] as bool;

// flag key for which decision was made
var flagKey = decideResponse.decision!.flagKey;

// user for which the decision was made
var userContext = decideResponse.decision!.userContext;

// reasons for the decision
var reasons = decideResponse.decision!.reasons;

Side effect

Invokes the decide notification listener if this listener is enabled.

Decide all

Returns decisions for all active (unarchived) flags for a user.

Minimum SDK version – 1.0.1

Description

Use the decide all method to retrieve decisions for all active flags before rendering content. This is particularly useful when you need to serve the correct cached version of your page or component based on the user's variation. For example when using an edge worker or cloud function.

Key features

  • Pre-rendering decision – Lets you know all variation assignments ahead of time.
  • Cache control – Lets you serve the correct cached content based on the user's pre-determined variations.
  • Delay experiment tracking – Use DISABLE_DECISION_EVENT to prevent the SDK from recording a decision before the user sees the feature. This ensures that participation is only tracked when the experience is delivered.

Parameters

The following table describes parameters for the decide all method:

ParameterTypeDescription
options (optional)SetSet of OptimizelyDecideOption enums.

Returns

Returns a map of OptimizelyDecisions. See OptimizelyDecision.

If the method fails for all flags (for example, the SDK is not ready or the user context is invalid), it returns a decision response with success status false, and reason and decision as null objects. If the method detects an error for a specific flag, it returns error messages in the reasons field of the decision for that flag.

Example decideAll

// make decisions for all active (unarchived) flags in the project for a user
Set<OptimizelyDecideOption> options = const { OptimizelyDecideOption.enabledFlagsOnly };
var decisionsResponse = await user!.decideAll(options);

var decisionMap = decisionsResponse.decisions;
var decisionForFlag1 = decisionMap["flag_1"];

Side effect

Invokes the DECISION notification listener for each decision, if this listener is enabled.

Decide for keys

Returns a map of flag decisions for specified flag keys.

Minimum SDK version – v1.0.1+

Description

Returns a map of decisions for the specified flag keys.

Parameters

The following table describes parameters for the decide for keys method:

ParameterTypeDescription
keysArrayArray of string flag keys.
options (optional)SetArray of OptimizelyDecideOption enums.

Returns

Returns a map of OptimizelyDecisions. For more information, see OptimizelyDecision.

If the method fails for all flags (for example, the SDK is not ready or the user context is invalid), it returns a decision response with success status false, reason and decision as null objects. If the method detects an error for a specific flag, it returns error messages in the reasons field of the decision for that flag.

Example decideForKeys

var keys = ["flag-1", "flag-2"];
var decisionsResponse = await user!.decideForKeys(keys);

var decisionMap = decisionsResponse.decisions;
var decision1 = decisionMap["flag_1"];
var decision2 = decisionMap["flag_2"];

Side effect

Invokes the decision notification listener for each decision, if this listener is enabled.

OptimizelyDecideOption

The following table lists the OptimizelyDecideOption enum with an explanation what happens if you set them. In addition to setting these options individually for a decide method, you can also set them as global defaults when you instantiate the Optimizely client. See Initialize Flutter SDK.

OptimizelyDecideOption enumIf set
OptimizelyDecideOption.DISABLE_DECISION_EVENTPrevents the SDK from dispatching an impression event when serving a variation. This disables decision tracking on the Optimizely Experiment Results page and the decision notification listener.
OptimizelyDecideOption.ENABLED_FLAGS_ONLYReturns decisions only for flags that are currently enabled. Used with the decide all method and decide for keys method.

When this option is not set, the Android SDK returns all decisions regardless of whether the flag is enabled.
OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICEBypasses the user profile service (both lookup and save) for the decision.

When this option is not set, user profile service overrides audience targeting, traffic allocation, and experiment mutual exclusion groups.
OptimizelyDecideOption.INCLUDE_REASONSAdds log messages to the reasons field of the decision. Critical errors are always returned, even if this option is not set.
OptimizelyDecideOption.EXCLUDE_VARIABLESExcludes flag variable values from the decision result. Use this option to minimize the returned decision by skipping large JSON variables.

The following code sample shows how to implement the OptimizelyDecideOption:

// Set options in a decide call
var flutterSDK = OptimizelyFlutterSdk("Your_SDK_Key"); // Replace with your SDK key
var response = await flutterSDK.initializeClient();

// Flag decision
var user = await flutterSDK.createUserContext(userId: "user123");
Set<OptimizelyDecideOption> options = {};
options.add(OptimizelyDecideOption.disableDecisionEvent);
var decisions = user!.decideAll(options);

Best practices

  • Early and late invocation
    • Use the decide all method early in your application's rendering process to load the correct cached content. For example, on the CDN with an Edge SDK.
    • Use the decide method at the point of user interaction or when you must ensure Feature Experimentation records the decision in your experiment analytics.
  • Combining methods – When using the decide and decide all methods, always pair the decide all method with the decide method for each experiment the user encounters. This prevents discrepancies between served content and analytics data.
  • Parameter management – Ensure you use the DISABLE_DECISION_EVENT option with the decide all method to avoid premature bucketing, then use the decide method to handle the decision event when the user experiences the content.

Source files

The language and platform source files containing the implementation for Flutter (Android) are at Optimizely.java and Flutter (Swift) are at OptimizelyClient.swift .