---
title: OptimizelyUserContext for the Flutter SDK
slug: feature-experimentation/docs/optimizelyusercontext-flutter
docTags: 
createdAt: 2026-08-05T20:39:53.000Z
---

The `OptimizelyUserContext` object lets you make flag decisions and track events for a user context you already created using the [Create User Context](docId:5k27nwFPeEn1-v7XWz1HT) method.

## OptimizelyUserContext minimum SDK version

`OptimizelyUserContext` is supported on the Optimizely Feature Experimentation Flutter SDK v1.0.1 or higher.

### Forced decision methods minimum SDK version

`setForcedDecision()`, `getForcedDecision()`, `removeForcedDecision()` and `removeAllForcedDecisions()` methods are supported on version 1.0.1 and higher.

### Real-Time Audiences for Feature Experimentation minimum SDK version

`fetchQualifiedSegments()` and `isQualifiedFor()` methods are supported on v2.0.0 and higher.

## OptimizelyUserContext definition

The following code shows the object definition for `OptimizelyUserContext`:

```dart
class OptimizelyUserContext {
  // set attributes for the user
  Future<BaseResponse> setAttributes(Map<String, dynamic> attributes)

  // make a decision about which flag variation the user buckets into for the flag key
  Future<DecideResponse> decide(String key, [Set<OptimizelyDecideOption> options = const {}])

  // make decisions about which flag variations the user buckets into for flag keys 
  Future<DecideForKeysResponse> decideForKeys(List<String> keys, [Set<OptimizelyDecideOption> options = const {}])

  // make decisions about which flag variations the user buckets into for all flags
  Future<DecideForKeysResponse> decideAll([Set<OptimizelyDecideOption> options = const {}])

  // track user event
  Future<BaseResponse> trackEvent(String eventKey, [Map<String, dynamic> eventTags = const {}])

  // sets a forced decision (variationKey) for a given decision context
  Future<BaseResponse> setForcedDecision(OptimizelyDecisionContext context, OptimizelyForcedDecision decision)

  // returns the forced decision (variationKey) for a given decision context
  Future<GetForcedDecisionResponse> getForcedDecision(OptimizelyDecisionContext context)

  // removes the forced decision (variationKey) for a given decision context
  Future<BaseResponse> removeForcedDecision(OptimizelyDecisionContext context)

  // removes all the forced decisions (variationKeys) for the user context
  Future<BaseResponse> removeAllForcedDecisions()

  //
  // The following methods require Real-Time Audiences for Feature Experimentation to be configured.
  //

  // An array of audience segment names that the user is qualified for. 
  // The result of **fetchQualifiedSegments()** is saved here. 
  // Can be null if not properly updated with fetchQualifiedSegments(). 
  // 
  //  You can read and write directly to the qualified segments array.   
  //  This lets you bypass the remote fetching process from ODP 
  //  or for utilizing your own fetching service.   
  Future<GetQualifiedSegmentsResponse> getQualifiedSegments()

  /// Sets qualified segments for the user context.
  ///
  /// Takes [qualifiedSegments] A [List] of strings specifying qualified segments for the user.
  /// Returns [BaseResponse]
  Future<BaseResponse> setQualifiedSegments(List<String> qualifiedSegments)

  /// Fetch all qualified segments for the user context.
  ///
  /// The segments fetched will be saved in **qualifiedSegments** and can be accessed any time using **getQualifiedSegments**.
  /// On failure, **qualifiedSegments** will be null and an error will be returned.
  /// Optional [options] A set of [OptimizelySegmentOption] for fetching qualified segments.
  /// Returns [BaseResponse]
  Future<BaseResponse> fetchQualifiedSegments([Set<OptimizelySegmentOption> options = const {}])

  /// Checks if the user is qualified for the given segment.
  ///
  /// Takes [segment] The segment name to check qualification for.
  /// Returns [BaseResponse]
  Future<BaseResponse> isQualifiedFor(String segment)
}
```

## Properties

The following table shows attributes for the `OptimizelyUserContext` object:

| Attribute             | Type   | Comment                                                                                                                                                                                                  |
| --------------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| (optional) userId     | String | The ID of the user                                                                                                                                                                                       |
| (optional) attributes | Map    | A map of custom key-value pairs specifying attributes for the user that are used for [audience targeting](docId\:UDBvr5a8WHNkyOqzPhOr9). You can pass the map with the user ID when you create the user. |

## Methods

The following table shows methods for the `OptimizelyUserContext` object:

| Method                      | Comment                                                                                                                                                                                                                                                                                                                                                                                                            |
| --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| setAttributes               | Pass the custom user attributes as key-value pairs in a map to the user context.                                                                                                                                                                                                                                                                                                                                   |
| decide                      | Returns a decide response which contains decision result for a flag key for a user. The system returns the decision result in a \`Decision\` object, and contains all data required to deliver the flag rule. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-flutter#decide)                                                                           |
| decide for specified keys   | Returns the decide for keys response object, which contains map of flag decisions for specified flag keys. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-flutter#decide-for-specified-keys)                                                                                                                                                           |
| decideAll                   | Returns the decide for keys response object, which contains decisions for all active (unarchived) flags for a user. See \[Decide methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-flutter#decide-all)                                                                                                                                                                 |
| trackEvent                  | Returns \`BaseResponse\` containing bool variable of success and string reason variable, which is empty in case of event tracked. Tracks a conversion event (in other words, an action a user takes) for a user. Logs an error message if the specified event key doesn't match any existing events. See \[Track Event]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/track-event-flutter) |
| setForcedDecision           | Forces a user into a specific variation. See \[Set Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-flutter#set-forced-decision-method---setforceddecision)                                                                                                                                                                                          |
| getForcedDecision           | Returns what variation the user was forced into. See \[Get Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-flutter#get-forced-decision-method---getforceddecision)                                                                                                                                                                                  |
| removeForcedDecision        | Removes a user from a specific forced variation. See \[Remove Forced Decision]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-flutter#remove-forced-decision-method---removeforceddecision)                                                                                                                                                                         |
| removeAllForcedDecisions    | Removes a user from all forced variations. See \[Remove All Forced Decisions]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/forced-decision-methods-flutter#remove-all-forced-decisions-method---removeallforceddecisions)                                                                                                                                                                 |
| fetchQualifiedSegments \*\* | Fetch all ODP real-time audiences that the user context qualified for. See \[Real-Time Audiences for Feature Experimentation segment qualification methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-flutter).                                                                                          |
| isQualifiedFor \*\*         | Check if the user context qualified for a given ODP real-time audience. See \[Real-Time Audiences for Feature Experimentation segment qualification methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-flutter).                                                                                         |
| getQualifiedSegments \*\*   | Returns \`GetQualifiedSegmentsResponse\` object containing an array of segment names that the user is qualified for. See \[Real-Time Audiences for Feature Experimentation segment qualification methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-flutter).                                            |
| setQualifiedSegments \*\*   | Sets qualified segments for the user context. See \[Real-Time Audiences for Feature Experimentation segment qualification methods]\(https\://docs.developers.optimizely.com/feature-experimentation/docs/real-time-audiences-for-feature-experimentation-segment-qualification-methods-flutter).                                                                                                                   |

\*\* Requires [Real-Time Audiences for Feature Experimentation](docId:6UBBxxRv331nBIVZ2lun8).

## See also

[Create User Context](docId:5k27nwFPeEn1-v7XWz1HT)

## Source files

The language and platform source files containing the implementation for the Flutter SDK are available on GitHub:

- **Android** – [Optimizely.java](https://github.com/optimizely/java-sdk/blob/master/core-api/src/main/java/com/optimizely/ab/Optimizely.java)
- **iOS** – [OptimizelyClient.swift](https://github.com/optimizely/swift-sdk/blob/master/Sources/Optimizely/OptimizelyClient.swift)
