Initialization
The Eppo SDK for Flutter Web is easy to initialize while offering robust customization options, making it adaptable to various use cases such as custom caching requirements and ultra-low-latency initialization.
The SDK is built on top of the Eppo JavaScript SDK, using dart:js_interop
to call the JavaScript SDK.
The Dart package is a wrapper around the JavaScript SDK, providing typed bindings.
Initialize the SDK
To complete basic initialization, you only need to provide an SDK key. Create an SDK key if you don't already have one.
Add the Eppo JavaScript SDK to your script tag:
<script src="https://cdn.jsdelivr.net/npm/@eppo/js-client-sdk@latest/dist/eppo-sdk.min.js"></script>
Add the Eppo Dart SDK to your pubspec.yaml
file:
dependencies:
eppo_web_sdk: ^1.0.0
Then, initialize the SDK:
import 'dart:js_interop';
import 'package:eppo_web_sdk/eppo_web_sdk.dart';
await eppo
.init(
EppoConfig(apiKey: '<SDK_KEY>').jsify() as JSAny,
)
.toDart;
Use the SDK instance
After initialization, you can use the SDK instance to assign a variation to a subject using the assignment methods.
final EppoClient client = eppo.getInstance();
final bool variation = client.getBooleanAssignment(
'<flag-key>',
'<subject-key>',
{}.jsify() as JSAny,
false,
);
Advanced Configuration
Basic initialization is great for most use cases, but the SDK provides options that you can use during initialization to customize the behavior of the SDK.
Initialization Options
How the SDK fetches, serves, and caches experiment configurations is configurable via the EppoClient
constructor:
apiKey
StringDefault: null
The SDK key for your environment.
assignmentLogger
AssignmentLoggerDefault: null
A callback that sends each assignment to your data warehouse. Required for experiment analysis.
banditLogger
BanditLoggerDefault: null
A callback that sends each bandit action to your data warehouse. Required for bandit analysis.
Example Configuration
Here's an example of initializing the SDK with custom configuration:
import 'dart:js_interop';
import 'dart:convert';
import 'package:eppo_web_sdk/eppo_web_sdk.dart';
// Initialize with custom configuration and wait for the client to be ready
await eppo
.init(
EppoConfig(
apiKey: '<SDK_KEY>',
assignmentLogger: AssignmentLogger(
logAssignment:
((JSAny event) {
final typedEvent = event as AssignmentEvent;
print('warehouse assignment logged:', typedEvent.toJson());
}
).toJS,
)
),
)
.toDart;
Configuration Caching
The SDK caches configurations in memory to ensure fast assignment evaluations. The cache is automatically updated based on the polling interval.
Best Practices
- Initialize Once: Create a single instance of the SDK at application startup and reuse it throughout your application.
- Wait for Readiness: Optionally, await
init
before making assignments to ensure configurations are loaded. - Implement Proper Logging: Configure a proper assignment logger to ensure experiment data is captured correctly.
- Use Subjects Consistently: Ensure that subject IDs are consistent across your application to maintain experiment integrity.