Monitor connection status
Register a connection status handler
Monitor changes in the connection status between the SDK and the server by registering a handler with NCEngine.addConnectionStatusHandler.
Method
Dart
static void addConnectionStatusHandler(String identifier, OnConnectionStatusChanged handler)
Parameters
| Parameter | Type | Description |
|---|---|---|
identifier | String | A unique ID for this handler. Used to remove it later. |
handler | OnConnectionStatusChanged | Callback receiving a ConnectionStatusChangedEvent. |
Code example
Dart
NCEngine.addConnectionStatusHandler('conn-listener', (event) {
print('Connection status changed: ${event.status}');
});
Remove the handler when it is no longer needed:
Dart
NCEngine.removeConnectionStatusHandler('conn-listener');
Get the current connection status
Use NCEngine.getConnectionStatus() when you need to read the current SDK connection state immediately, for example during app startup or when restoring UI state after returning to the foreground.
Method
Dart
static Future<ConnectionStatus> getConnectionStatus()
Code example
Dart
final status = await NCEngine.getConnectionStatus();
print('Current connection status: $status');
ConnectionStatus values
| Value | Description |
|---|---|
ConnectionStatus.connected | Connected to the server |
ConnectionStatus.connecting | Connecting |
ConnectionStatus.unconnected | Not connected to the server |
ConnectionStatus.kickedOfflineByOtherClient | Kicked offline by another device |
ConnectionStatus.tokenIncorrect | Token is invalid or expired |
ConnectionStatus.networkUnavailable | No network available |
ConnectionStatus.connUserBlocked | The user has been blocked by the server |
ConnectionStatus.signOut | The user has signed out |
ConnectionStatus.suspend | The connection is suspended, for example when the app goes to the background |
ConnectionStatus.timeout | The connection attempt timed out |
ConnectionStatus.unknown | An unknown connection status |
tip
Register the connection status handler before calling NCEngine.connect() to avoid missing the initial connected event.