alrightokay
03/21/2025, 5:21 AMShubham Bajaj
03/22/2025, 7:33 AMspeech-start and speech-end, in the Flutter SDK, these events are delivered through a more generalized event system, specifically within the message event type.
### How Events Work in Flutter SDK
According to the documentation found on GitHub for the Flutter SDK, events are handled like this:
dart
vapi.onEvent.listen((event) {
if (event.label == "call-start") {
print('call started');
}
if (event.label == "call-end") {
print('call ended');
}
// Speech statuses, function calls and transcripts will be sent via messages
if (event.label == "message") {
print(event.value);
}
});
The key note here is that "Speech statuses, function calls and transcripts will be sent via messages". This means that unlike the Web SDK, which has dedicated event types, the Flutter SDK delivers speech updates within the message event.
### Speech Update Message Structure
From the codebase search results, I found that speech updates have a specific structure. When you receive a message event, you need to check if it's a speech update by examining its type:
dart
if (event.label == "message") {
var message = event.value;
if (message['type'] == 'speech-update') {
// This is a speech update message
String status = message['status']; // Will be either 'started' or 'stopped'
String role = message['role']; // Will be either 'assistant' or 'user'
// Handle the speech update accordingly
if (status == 'started' && role == 'assistant') {
// Assistant started speaking
} else if (status == 'stopped' && role == 'user') {
// User stopped speaking
}
}
}
### Speech Update States
From the code search, I found that speech updates have these fields:
1. `type`: Will be 'speech-update'
2. `status`: Can be either 'started' or 'stopped'
3. `role`: Can be either 'assistant' or 'user'
This means you can detect four states:
- Assistant speech started (status: 'started', role: 'assistant')
- Assistant speech stopped (status: 'stopped', role: 'assistant')
- User speech started (status: 'started', role: 'user')
- User speech stopped (status: 'stopped', role: 'user')
### Android SDK Example
For reference, I also found how this is implemented in the Android SDK, which may give you a clearer picture of how to handle these events:
kotlin
lifecycleScope.launch {
vapi.eventFlow.collect { event ->
when (event) {
is Vapi.Event.SpeechUpdate -> println("Speech update: ${event.status}, ${event.role}")
// Other event types...
}
}
}
In the Android SDK, speech updates are handled as a specific event type rather than through the generic message event. However, the underlying data structure (with status and role fields) appears to be the same.
## Conclusion
To detect speech start and end events in your Flutter application using the VAPI SDK, you need to:
1. Listen for events with vapi.onEvent.listen
2. Filter for events where event.label == "message"
3. Check if the message has type: 'speech-update'
4. Check the status ('started' or 'stopped') and role ('assistant' or 'user') to determine the specific speech state