Home
/
Chat
/
Flutter

User & channel metadata

With metadata and metacounter which consist of key-value items, you can store additional information to users and channels. This page explains how to manage user metadata, channel metadata, and channel metacounter.


User metadata

You can store additional information to users such as phone number, email or a long description of a user with user metadata, which can be fetched or rendered into the UI. A user metadata is a Map<String, String> and it can be stored into a User object.

Create user metadata

To store a user metadata into a User object, add key-value items after creating a Map<String, String>, and pass it as an argument to a parameter when calling the createMetaData method. You can put multiple key-value items in the dictionary.

Light Color Skin
Copy
try {
    final user = sendbird.currentUser();
    final data = ['key1': 'value1', 'key2': 'value2'];
    final createdData = await user.createMetaData(data);
    // A User’s metadata has been created.
} catch (e) {
    // Handle error.
}

Update user metadata

The procedure to update a user metadata is the same as creating a user metadata. Values of existing keys will be updated and values of new keys will be added.

Light Color Skin
Copy
try {
    final user = sendbird.currentUser();
    final data = ['key1': 'valueToUpdate1', 'key2': 'valueToUpdate2'];
    final updatedData = await user.updateMetaData(data);
    // A User’s meta data has been updated.
} catch (e) {
    // Handle error.
}

Retrieve user metadata

You can retrieve user metadata by reading the metadata property of a User object.

Light Color Skin
Copy
final user = sendbird.currentUser();
final metaData = user.metaData;

Delete user metadata

You can delete a user metadata as below.

Light Color Skin
Copy
try {
    final user = sendbird.currentUser();
    await user.deleteMetaData('key1');
} catch (e) {
    // Handle error.
}

Channel metadata

You can store additional information to channels such as background color or a long description of a channel with channel metadata, which can be fetched or rendered into the UI. A channel metadata is a Map<String, String> and it can be stored into a OpenChannel or a GroupChannel object.

Create channel metadata

To store a channel metadata into a channel objeact, add key-value items after creating a Map<String, String>, and pass it as an argument to a parameter when calling the createMetaData() method. You can put multiple key-value items in the dictionary.

Light Color Skin
Copy
try {
    final createdMetaData = await groupChannel.createMetaData(
        {'key1': 'value1', 'key2': 'value2'}
    );
    // New metadata is applied to groupChannel.
} catch (e) {
    // Handle error.
}

Update channel metadata

The procedure to update a channel metadata is the same as creating a channel metadata. Values of existing keys will be updated and values of new keys will be added.

Light Color Skin
Copy
try {
    final updatedMetaData = await groupChannel.updateMetaData({
        'key1': 'valueToUpdate1', // Update an existing item with new value.
        'key2': 'valueToUpdate2', // Update an existing item with new value.
        'Key3': 'valueToUpdate3'    // Add a new key-value item.
    });
    // Updated metadata is applied to groupChannel.
} catch (e) {
    // Handle error.
}

Retrieve channel metadata

You can retrieve channel metadata by creating a List of keys to retrieve and passing it as an argument to a parameter in the getMetaData() method. A Map<String, String> will return through the result with corresponding key-value items.

Light Color Skin
Copy
try {
    final metaData = await groupChannel.getMetaData(['key1', 'key2']);
} catch (e) {
    // Handle error.
}

Retrieve cached channel metadata

When Sendbird Chat SDK detects any of the create, read, update, and delete operations on the channel metadata, the SDK caches the metadata. The cached metadata is also updated whenever a channel list is fetched.

You can retrieve the metadata through the getCachedMetaData() method without having to query the server.

Light Color Skin
Copy
final metaData = channel.getCachedMetaData();

Delete channel metadata

You can delete a channel metadata through the deleteMetaData() method as below.

Light Color Skin
Copy
try {
    await groupChannel.deleteMetaData('key1');
    // metadata with given key has been deleted from the groupChannel.
} catch (e) {
    // Handle error.
}

Channel metacounter

You can store additional information to channels such as the tracking number of likes with channel metacounter, which can be fetched or rendered into the UI. A channel metacounter is a Map<String, int> and it can be stored into a OpenChannel or a GroupChannel object.

Note : A channel metacounter is primarily used to track and update discrete indicators in a channel. Use channel metacounter instead of channel metadata when you need to store integer data for arithmetical increasing and decreasing operations.

Create channel metacounter

To store a metacounter into a channel, add key-value items after creating a Map<String *, int>, and pass it as an argument to a parameter when calling the createMetaCounters() method. You can put multiple key-value items in the dictionary.

Light Color Skin
Copy
try {
    final metaCounters = await groupChannel.createMetaCounters({'key1': 1, 'key2': 2});
    // New meta counters are applied to groupChannel.
} catch (e) {
    // Handle error.
}

Update channel metacounter

The procedure to update a channel metacounter is the same as creating a channel metacounter. Through the deleteMetaCounters() method, values of existing keys can be updated and values of new keys will be added.

Light Color Skin
Copy
try {
    final metaCounters = await groupChannel.updateMetaCounters({'key1': 3, 'key2': 4});
    // Updated meta counters are applied to groupChannel.
} catch (e) {
    // Handle error.
}

Increase in channel metacounter

You can increase values in a channel metacounter by passing a Map<String, int> of items to increase as an argument to a parameter in the increaseMetaCounters() method. The values of corresponding keys in the metacounter will be incremented by the number you’ve specified.

Light Color Skin
Copy
try {
    final metaCounters = await groupChannel.increaseMetaCounters({'key1': 5, 'key2': 2});
    // If the value of key1 was 3, it's 8 now.
} catch (e) {
    // Handle error.
}

Decrease in channel metacounter

You can decrease values in a channel metacounter by passing a Map<String, int> of items to decrease as an argument to a parameter in the decreaseMetaCounters() method. The values of corresponding keys in the metacounter will be decremented by the number you’ve specified.

Light Color Skin
Copy
try {
    final metaCounters = await groupChannel.decreaseMetaCounters({'key1': 5, 'key2': 2});
    // if the value of key1 was 8, it's 3 now.
} catch (e) {
    // Handle error.
}

Retrieve channel metacounter

You can retrieve channel metacounter by creating a list of keys to retrieve and passing it as an argument to a parameter in the getMetaCounters() method. A Map<String, int> will return with corresponding key-value items.

Light Color Skin
Copy
try {
    final metaCounters = await groupChannel.getMetaCounters(['key1', 'key2']);
} catch (e) {
    // Handle error.
}

Delete channel metacounter

The process for deleting a metacounter is as simple as creating one. Use the deleteMetaCounters() to delete the channel metacounter.

Light Color Skin
Copy
try {
    await groupChannel.deleteMetaCounters('key1');
} catch (e) {
    // Handle error.
}