Skip to content

Reference

Exported functions

deviceIdentifier()

Returns the native TrainingKit device identifier.

dart
Future<String> deviceIdentifier()

The identifier is stable for a given device installation. Use it as the value of the X-TrainingKit-Device HTTP header on every GraphQL request.

Throws a TrainingKitException if the native module is unavailable (for example, if the app was not rebuilt after adding the package).


launchWorkout(session)

Launches the native workout experience from a TrainingKit session payload.

dart
Future<void> launchWorkout(TrainingKitSession session)

The function inspects session['__typename'] and session['format'] to determine which native flow to launch:

ConditionNative flow
__typename == 'WorkoutBlockSession' or format == 'CLASSIC'Classic workout
__typename == 'WorkoutVideoSession' or format == 'PLAY'Video workout

Throws a TrainingKitException if trainingKitToken is missing or if the session type cannot be identified.


workoutEvents()

Returns a broadcast stream of workout lifecycle events emitted by the native SDKs.

dart
Stream<TrainingKitEvent> workoutEvents()

Start listening before calling launchWorkout(session) so save, quit, and tracking events cannot be missed. Cancel the subscription when the host screen no longer needs workout events.

dart
final subscription = workoutEvents().listen((event) {
  switch (event.type) {
    case TrainingKitEventType.save:
      // Persist completed workout state from event.data.
      break;
    case TrainingKitEventType.quit:
      // The user exited before completing the workout.
      break;
    case TrainingKitEventType.event:
      // Forward analytics metadata if needed.
      break;
  }
});

await subscription.cancel();
Event typeTriggerPayload
TrainingKitEventType.saveThe workout completed and produced session state.event.data, mirroring native SaveWorkoutState (richer on iOS than Android).
TrainingKitEventType.quitThe user quit without completing the workout.No payload.
TrainingKitEventType.eventTrainingKit emitted a tracking event.event.name and event.properties.

Types

TrainingKitSession

dart
typedef TrainingKitSession = Map<String, dynamic>;

The session payload required by launchWorkout. The full GraphQL session payload (which may contain many additional fields) satisfies this type and is encoded to JSON and passed through to the native SDK.

At minimum it must contain a trainingKitToken, plus either __typename or format so the workout kind can be identified.

TrainingKitException

dart
class TrainingKitException implements Exception {
  const TrainingKitException(this.message);
  final String message;
}

Thrown by the bridge when a workout cannot be launched or the device identifier cannot be read.

TrainingKitEvent

dart
class TrainingKitEvent {
  const TrainingKitEvent({
    required this.type,
    this.data,
    this.name,
    this.properties,
  });

  final TrainingKitEventType type;
  final Map<String, dynamic>? data;
  final String? name;
  final Map<String, dynamic>? properties;
}

data is set for save events, while name and properties are set for analytics events.

TrainingKitEventType

dart
enum TrainingKitEventType {
  save,
  quit,
  event,
}

Method channel

The package communicates with the native side over a single Flutter method channel named trainingkit_flutter. It is registered automatically on both platforms via Flutter plugin autolinking.

The underlying native methods are not intended to be called directly:

MethodDescription
deviceIdentifierReturns the native device ID
launchClassicWorkout (jsonString, token)Launches the classic workout controller
launchVideoWorkout (jsonString, token)Launches the video workout controller

Workout lifecycle events are delivered over a separate event channel named trainingkit_flutter/events.

Use deviceIdentifier() and launchWorkout() from the package instead.

Error messages

MessageCause
TrainingKit returned an empty device identifier…The native module returned no identifier. The app needs a native rebuild after adding the package.
TrainingKit session is missing trainingKitToken.The session object passed to launchWorkout does not contain trainingKitToken. Check that the full session payload was fetched first.
Unsupported TrainingKit session type: <type>.The __typename and format fields do not match any known workout kind. Verify the GraphQL response.
MissingPluginException (from Flutter)The plugin was not registered. Do a full rebuild (flutter clean + flutter run).