Skip to main content

Installation

Add the Flutter package:
pubspec.yaml
dependencies:
  spatius: ^1.2.0
Then install dependencies:
flutter pub get
The package supports iOS and Android Flutter apps.

Import

import 'package:spatius/avatar_kit.dart' hide ConnectionState, Transform;

Initialize

Initialize AvatarKit once before loading avatars or creating an avatar view.
await AvatarSDK.initialize(
  appID: appId,
  configuration: Configuration(
    region: 'us-west',
    audioFormat: const AudioFormat(sampleRate: 16000),
    drivingServiceMode: DrivingServiceMode.direct,
    logLevel: LogLevel.all,
  ),
);

await AvatarSDK.setSessionToken(sessionToken);
Use DrivingServiceMode.direct for Direct Mode. Use DrivingServiceMode.backend when your Flutter app receives encoded audio payloads and motion data payloads from a Backend Mode.

Load an avatar

final avatar = await AvatarManager.shared.load(
  id: avatarId,
  onProgress: (progress) {
    // Update loading UI.
  },
);

Render and control playback

The Flutter view creates an AvatarController when the platform view is ready. Keep that controller and use it for lifecycle, state, and audio operations.
void onAvatarViewCreated(AvatarController controller) {
  controller.onConnectionState = (state, errorMessage) {
    // Observe Direct Mode connection state.
  };

  controller.onConversationState = (state) {
    // Observe avatar playback state.
  };

  controller.onError = (error) {
    // Log or surface AvatarError.
  };
}
For Direct Mode audio input, start the connection and send PCM chunks:
await controller.start();
controller.send(audioBytes, end: isLastChunk);
For audio source and timing guidance, see Audio. For Backend Mode input, feed encoded audio payloads and motion data payloads received from your backend:
final conversationId = await controller.yieldAudioData(audioBytes, end: isLastChunk);
controller.yieldAnimations(framesData, conversationID: conversationId);

Demos

Flutter Direct Mode demo

Direct Mode sample with bundled PCM files.

Flutter Backend Mode demo

Flutter client that talks to the Backend Mode reference server.