Skip to main content
Version: 5.x.x

Leave or End Room - Flutter

Participant can choose to leave the room without removing all the other participants. This is typically done by Leave Room.

Alternatively, if the participant is the host or the last person remaining in the session, they can choose End Room by removing all other participants, which will end the session for everyone.

leave()​

To leave the room without removing all the participant you need to call leave() on the Room object.

leave() returns a Future that throws a VideoSDKError when the SDK refuses it. Calling it on a room that has already left the room throws 3054.

end()​

To leave the room by removing all the participant you need to call end() on the Room object.

end() returns a Future that throws a VideoSDKError when the SDK refuses it, for example 3035 when the room has already left.

After the room​

You do not release the room yourself. Once Events.roomLeft is delivered, the room removes every event handler and releases itself.

  • leave() can complete before Events.roomLeft arrives. Close your room screen from the event's handler.
  • A room that has left cannot join again. Create a new room for the next room.

Example​

import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class RoomScreen extends StatefulWidget {
...
}

class _RoomScreenState extends State<RoomScreen> {
late Room _room;

@override
void initState() {
...
}

Future<void> leaveRoom() async {
try {
await _room.leave();
} on VideoSDKError catch (error) {
// 3054: the room has already left.
if (error.code != 3054) {
print("Leave failed: ${error.code} ${error.name}");
}
}
}

Future<void> endRoom() async {
try {
await _room.end();
} on VideoSDKError catch (error) {
print("End failed: ${error.code} ${error.name}");
}
}

@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed: leaveRoom,
child: const Text("Leave Room"),
),
ElevatedButton(
onPressed: endRoom,
child: const Text("End Room"),
),
]
);
}

}
tip

You should also call leave() when the user closes your main room widget without tapping Leave Room, for example with the back button, so that the room is left once the widget is closed. The Quick Start does this from a PopScope callback.

Events associated with Leave​

Following callbacks are received when a participant leaves the room.

Events associated with End​

Following callbacks are received when a participant ends the room.

import 'package:flutter/material.dart';
import 'package:videosdk/videosdk.dart';

class RoomScreen extends StatefulWidget {
...
}

class _RoomScreenState extends State<RoomScreen> {
late Room _room;

@override
void initState() {
...

setupRoomEventListener();
}

@override
Widget build(BuildContext context) {
return YourRoomWidget();
}

void setupRoomEventListener() {
_room.on(Events.roomLeft, (LeaveReason? reason) {
// Room left. Close the room screen here.
});
_room.on(Events.participantLeft, (Participant participant, LeaveReason reason) {
// Participant left the room
});
}
}

API Reference​

The API references for all the methods and events utilized in this guide are provided below.

Got a Question? Ask us on discord