Skip to main content
Version: 5.x.x

RealtimeStore Methods - Flutter

setValue()​

The setValue() method is used to store or update data in the RealtimeStore. If a key already exists, it will be overwritten with the new value. Passing null as the value deletes the key.

Key Notes​

  • Supports String values only.
  • Each key can store up to 1 KB of data (1024 bytes of UTF-8).
  • A maximum of 100 keys are allowed per session.
  • Deletion is performed by setting the value to null.
  • Throws a VideoSDKError if:
    • The key is empty or only whitespace (3073).
    • The value is empty or only whitespace (3073). Use null to delete a key.
    • The value is larger than 1 KB (3072).
    • The room has not been joined yet. This throws a VideoSDKError with code 3035.
    • The request fails. This throws a VideoSDKError, for example 4045 (FAILED_TO_SET_VALUE), or 3065 (ERROR_ROOM_RECONNECTING) while the room reconnects.
    • The session exceeds the 100-key limit.

Parameters​

  • key (String)
    The unique key to store the data under.

  • value (String?)
    The string value to be stored. Pass null to delete the key.

Returns​

  • Future<void>

Example​

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

class RoomScreen extends StatefulWidget {
//Existing configuration
}

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

@override
void initState() {
super.initState();
_room = VideoSDK.createRoom(
roomId: widget.roomId,
token: widget.token,
participantName: "User1",
micEnabled: true,
camEnabled: true,
defaultCamera: FacingMode.FRONT,
multiStream: true,
mode: Mode.SEND_AND_RECV,
);
_joinRoom();
}

Future<void> _joinRoom() async {
try {
await _room.join();
} on VideoSDKError catch (e) {
print("Join failed: ${e.code} ${e.name}");
}
}

@override
Widget build(BuildContext context) {
return Column(children: [
ElevatedButton(
onPressed: () async {
String key = "BLOCK_CHAT";
String value = "true";
try {
await _room.realtimeStore.setValue(key, value);
} catch (e) {
print("Error:$e");
}
},
child: const Text("Realtime Store Set Value"),
),
]);
}
}

getValue()​

The getValue() method retrieves the current value associated with a given key.

Key Notes​

  • Returns the current string value for the given key, or null when the server answers with no value. A key that is not set throws 4046.
  • Useful for fetching shared state when joining a room or reloading a view.
  • Throws a VideoSDKError: 3073 for an empty key, 3035 if the room has not been joined yet, 4046 (FAILED_TO_GET_VALUE) when the key is not set or was cleared, or another code if the request fails.

Parameters​

  • key (String)
    The key whose value you want to retrieve. It must not be empty.

Returns​

  • Future<String?> - The stored value, or null when the server answers with no value. A key that is not set throws 4046.

Example​

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

class RoomScreen extends StatefulWidget {
//Existing configuration
}

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

@override
void initState() {
super.initState();
_room = VideoSDK.createRoom(
roomId: widget.roomId,
token: widget.token,
participantName: "User1",
micEnabled: true,
camEnabled: true,
defaultCamera: FacingMode.FRONT,
multiStream: true,
mode: Mode.SEND_AND_RECV,
);
_joinRoom();
}

Future<void> _joinRoom() async {
try {
await _room.join();
} on VideoSDKError catch (e) {
print("Join failed: ${e.code} ${e.name}");
}
}

@override
Widget build(BuildContext context) {
return Column(children: [
ElevatedButton(
onPressed: () async {
String key = "BLOCK_CHAT";
try {
String? response = await _room.realtimeStore.getValue(key);
setState(() => _response = response);
} catch (e) {
print("Error:$e"); // 4046 when the key is not set
}
},
child: Text("Realtime Store Get Value: $_response"),
),
]);
}
}

observe()​

The observe() method subscribes to real-time updates for a given key. When the key’s value changes, your callback runs for every participant observing that key.

Key Notes​

  • The callback itself identifies the observation. Pass the same function to stopObserving().
  • Observing a key subscribes to it on the server, so every change, from any participant, reaches your listener until you stop observing.
  • Observing a key again with the same callback has no effect.
  • The callback receives the new value and information about who updated it.
  • Ideal for syncing live data like timers, shared notes, or agendas.
  • Throws a VideoSDKError with code 3035 if the room has not been joined yet.
  • Throws a VideoSDKError if the request fails, for example 3065 (ERROR_ROOM_RECONNECTING) while the room reconnects.

Parameters​

  • key (String)
    The key to observe. An empty key observes nothing.

  • callback (void Function(String? value, Participant? updatedBy))
    Function triggered on each update. Receives:

    • value (String?) → Updated value or null if deleted
    • updatedBy (Participant?) → Object of participant who made the update, or null if that participant is no longer in the room

Returns​

  • Future<void>

Example​

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

class RoomScreen extends StatefulWidget {
//Existing configuration
}

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

void _onBlockChat(String? value, Participant? updatedBy) {
// updatedBy is null if that participant is no longer in the room
print("New value: $value, updated by: ${updatedBy?.id}");
}

@override
void initState() {
super.initState();
_room = VideoSDK.createRoom(
roomId: widget.roomId,
token: widget.token,
participantName: "User1",
micEnabled: true,
camEnabled: true,
defaultCamera: FacingMode.FRONT,
multiStream: true,
mode: Mode.SEND_AND_RECV,
);
_joinRoom();
}

Future<void> _joinRoom() async {
try {
await _room.join();
} on VideoSDKError catch (e) {
print("Join failed: ${e.code} ${e.name}");
}
}

@override
Widget build(BuildContext context) {
return Column(children: [
ElevatedButton(
onPressed: () async {
String key = "BLOCK_CHAT";
try {
await _room.realtimeStore.observe(key, _onBlockChat);
} catch (e) {
print("Error:$e");
}
},
child: const Text("Realtime Store Observe"),
),
]);
}
}

stopObserving()​

Stops a callback that observe() registered for a key.

Key Notes​

  • Should be called when the listener is no longer needed, for example when the widget that shows the value is disposed.
  • The callback stops at once. When it was the key's last observer, the SDK also stops observing the key, which needs a joined room. Once Events.roomLeft has fired, the callback is still removed, but the call throws a VideoSDKError with code 3035. In teardown code such as State.dispose(), where you cannot await, add a .catchError() handler to the call.
  • A callback that is not observing the key is ignored.

Parameters​

  • key (String)
    The key you observed.

  • callback (void Function(String? value, Participant? updatedBy))
    The same function you passed to observe().

Returns​

  • Future<void>

Example​

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

class RoomScreen extends StatefulWidget {
//Existing configuration
}

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

// The callback passed to observe()
void _onBlockChat(String? value, Participant? updatedBy) {}

@override
void initState() {
super.initState();
_room = VideoSDK.createRoom(
roomId: widget.roomId,
token: widget.token,
participantName: "User1",
micEnabled: true,
camEnabled: true,
defaultCamera: FacingMode.FRONT,
multiStream: true,
mode: Mode.SEND_AND_RECV,
);
_joinRoom();
}

Future<void> _joinRoom() async {
try {
await _room.join();
} on VideoSDKError catch (e) {
print("Join failed: ${e.code} ${e.name}");
}
}

@override
Widget build(BuildContext context) {
return Column(children: [
ElevatedButton(
onPressed: () async {
try {
await _room.realtimeStore.stopObserving("BLOCK_CHAT", _onBlockChat);
} catch (e) {
print("Error:$e");
}
},
child: const Text("Realtime Store Stop Observing"),
),
]);
}
}

Got a Question? Ask us on discord