Friend management
Prerequisites
The managed hosting service is enabled by default.
Friend event listeners
Register a UserHandler before calling NCEngine.connect to receive friend events:
| Callback | Trigger |
|---|---|
onFriendAdd | Friendship established |
onFriendDelete | Friendship removed |
onFriendCleared | Server cleared the friend list |
onFriendInfoChangedSync | Friend remark or extension updated on another device |
onFriendApplicationStatusChanged | Friend request status changed |
Dart
NCEngine.addUserHandler('friend-handler', UserHandler(
onFriendAdd: (event) {
print('Friend added: ${event.userId}');
},
onFriendDelete: (event) {
print('Friends deleted: ${event.userIds}');
},
onFriendInfoChangedSync: (event) {
print('Friend info changed: ${event.userId}');
},
onFriendApplicationStatusChanged: (event) {
print('Friend request status: ${event.applicationStatus} for ${event.userId}');
},
));
Add a friend
Dart
await NCEngine.user.addFriend(
AddFriendParams(userId: 'user1', extra: 'Hello'),
(processCode, error) {
if (error == null) print('Friend request sent');
},
);
Set friend request permission
| Enum | Description |
|---|---|
| free | Accept all requests |
| needVerify | Require approval |
| noOneAllowed | Reject all requests |
Dart
await NCEngine.user.setFriendAddPermission(FriendAllowType.needVerify, (error) {
if (error == null) print('Permission updated');
});
Remove friends
Dart
await NCEngine.user.deleteFriends(
DeleteFriendsParams(userIds: ['user1', 'user2']),
(error) {
if (error == null) print('Friends removed');
},
);
Check friendship
Dart
await NCEngine.user.checkFriends(
CheckFriendsParams(userIds: ['user1', 'user2']),
(relationList, error) {
if (error == null) print('Friend relations: $relationList');
},
);
Handle friend requests
Get friend request list (paginated)
Dart
final query = NCEngine.user.createFriendApplicationsQuery(
FriendApplicationsQueryParams(
applicationTypes: [FriendApplicationType.received],
status: [FriendApplicationStatus.unhandled],
pageSize: 20,
),
);
await query.loadNextPage((result, error) {
if (error == null) {
print('Applications: ${result?.data}');
print('Total matched: ${result?.totalCount}');
}
});
Accept a friend request
Dart
await NCEngine.user.acceptFriendApplication('user1', (error) {
if (error == null) print('Request accepted');
});
Decline a friend request
Dart
await NCEngine.user.refuseFriendApplication('user1', (error) {
if (error == null) print('Request declined');
});
Get friend list
Dart
await NCEngine.user.getFriends((friends, error) {
if (error == null) print('Friends: $friends');
});
Set friend info
Dart
final friendInfo = FriendInfo(userId: 'user1', remark: 'Nickname');
await NCEngine.user.setFriendInfo(friendInfo, (error) {
if (error == null) print('Friend info updated');
});
Get friend info by IDs
Dart
await NCEngine.user.getFriendsInfo(['user1', 'user2'], (friends, error) {
if (error == null) print('Friend info: $friends');
});
Search friends by name
Dart
await NCEngine.user.searchFriendsInfo('keyword', (friends, error) {
if (error == null) print('Search results: $friends');
});