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

Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Set up notification listener

Describes how to set up and remove notification listeners using the Full Stack Flutter SDK.

Notification listeners trigger a callback function that you define when the SDK triggers based on certain actions.

The most common use case is to send a stream of all feature flag decisions to an analytics provider or to an internal data warehouse to join it with other data that you have about your users.

Notification listener types

For more information about notification listener types and use cases, see Notification listeners.

For code samples, see the following sections.

Add or remove all notification listeners

The example code below shows how to add a listener, remove a listener, remove all listeners of a specific type (such as all decision listeners) and remove all listeners.

// Add Notification Listener (LogEvent)
    var logEventListenerId =
        await flutterSDK?.addLogEventNotificationListener((logEvent) {
          print(logEvent.url);
          print(logEvent.params);
        });
// Remove Notification Listener
    await flutterSDK?.removeNotificationListener(logEventListenerId);

// Clear all notification listeners of a certain type
    await flutterSDK?.clearNotificationListeners(ListenerType.logEvent);

// Clear all notifications
    await flutterSDK?.clearAllNotificationListeners();

Set up each type of notification listener

The example code below shows how to set up each type of notification listener.

// SET UP ACTIVATE NOTIFICATION LISTENER
var notificationId = await flutterSDK?.addActivateNotificationListener((activateNotification) {
  print(activateNotification.experiment["key"]);
  print(activateNotification.userId);
  print(activateNotification.attributes);
  print(activateNotification.variation["key"]);
  // Send data to analytics provider here
});

// SET UP DECISION NOTIFICATION LISTENER
var decisionEventListenerId = await flutterSDK?.addDecisionNotificationListener((notification) {
// Access type on decisionObject to get type of decision
var decisionType = notification.type;
  if (decisionType == "flag") {
    var flagDecisionInfo = notification.decisionInfo;
    var flagKey = flagDecisionInfo["flagKey"];
    var enabled = flagDecisionInfo["enabled"];
    var decisionEventDispatched = flagDecisionInfo["decisionEventDispatched"];
  }
    // Send data to analytics provider here
});

// SET UP LOG EVENT NOTIFICATION LISTENER
var logEventListenerId = await flutterSDK?.addLogEventNotificationListener((logEvent) {
    // process the logEvent object here (send to analytics provider, audit/inspect data)
});

// SET UP OPTIMIZELY CONFIG NOTIFICATION LISTENER
var updateConfigListenerId = await flutterSDK?.addConfigUpdateNotificationListener((configNotification) { 
    var optimizelyConfig = flutterSDK.getOptimizelyConfig();
});

// SET UP TRACK LISTENER
var trackNotificationListenerId = await flutterSDK?.addTrackNotificationListener((trackNotification) { 
    // process the event here (send to analytics provider, audit/inspect data)
});