---
title: Create a user context using the Flutter SDK
slug: feature-experimentation/docs/create-user-context-flutter
docTags: 
createdAt: 2026-08-05T20:39:56.000Z
---

The purpose of this method is to create a user and set user context once, so you do not have to specify the user each time you make a flag decision or track an event. You can define multiple user contexts. The system returns user context as a runtime object that is not otherwise persisted.

## Version

1.0.1 or higher

## Description

This call creates a user context for flag decisions and events. You can call this method on the Optimizely client instance, even before you have fully configured the instance.

## Parameters

The following table lists the required and optional parameters:

| Parameter                    | Type   | Description                                                                                                                                                 |
| ---------------------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **userId** \*required\*      | String | The ID of the user.                                                                                                                                         |
| **attributes**\ \*optional\* | Map    | A map of custom key-value pairs specifying attributes for the user that the system uses for audience targeting. See the following section for more details. |

## Audience attributes

Set custom audience attributes for the user, which you can use to [target audiences](docId\:UDBvr5a8WHNkyOqzPhOr9). You can pass strings, numbers, Booleans and null as custom user attribute values. If you want to target audiences based on the version of your application they are using, you can also pass in a string formatted as a [semantic version](https://semver.org/), then define a `version`  audience condition in the Optimizely app.

:::hint{type="warning"}
**Important**
During audience evaluation, if you do not pass a valid attribute value for a given audience condition—for example, if you pass a string when the audience condition requires a Boolean or if you forget to pass a value—then the system skips that condition. The [SDK logs](docId:04MtzZIVCCMa8uyxK7kjy) include warnings when this occurs.
:::

## Returns

Returns an OptimizelyUserContext object. For details, see [OptimizelyUserContext](docId\:qkagdzXifMComMybuU4ls).

## Example

```dart
// Option 1 – Create a user, then set attributes.
var user = await flutterSDK.createUserContext("user123");
var attributes = <String, dynamic>{};
attributes["is_logged_in"] = false;
attributes["app_version"] = "1.3.2";
user!.setAttributes(attributes);

// Option 2 – Pass attributes when creating the user.
var attributes = <String, dynamic>{};
attributes["is_logged_in"] = false;
attributes["app_version"] = "1.3.2";
var optimizelyUserContext = await optimizelyClient.createUserContext(userId: "user123", attributes: attributes);
```

## See Also

[OptimizelyUserContext](docId\:qkagdzXifMComMybuU4ls)

## Source files

The language and platform source files containing the implementation for the Flutter SDK is available on GitHub.

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