Skip to main content

Initialization

EngineProvider is the Chat UI bridge for engine initialization, connection state, incoming messages, channel refresh events, local notifications, total unread count, and global feature flags.

Create providers

Create EngineProvider once for the app session and pass it to NexconnChatUIProviders.

Dart
final engineProvider = EngineProvider();

runApp(
NexconnChatUIProviders(
engineProvider: engineProvider,
child: const MyApp(),
),
);

Inside widgets, read the same provider from BuildContext.

Dart
final engineProvider = context.read<EngineProvider>();

Initialize

Call initialize before connecting the current user.

Dart
await engineProvider.initialize(
InitParams(appKey: appKey),
);

Pass optional deployment or endpoint settings through InitParams when your Nexconn environment requires them. The App Key, token, areaCode, and endpoint overrides must all belong to the same release environment.

Dart
await engineProvider.initialize(
InitParams(
appKey: appKey,
areaCode: AreaCode.sg,
),
);

For private deployments, configure the endpoint fields supplied by Nexconn.

Dart
await engineProvider.initialize(
InitParams(
appKey: appKey,
naviServer: naviServer,
fileServer: fileServer,
statisticServer: statisticServer,
logServer: logServer,
),
);

Connect

Connect after your app has a valid user token from your server. connect returns a numeric result code and also delivers the result through the callback. Check both to handle all failure paths.

Dart
NCError? callbackError;
String? connectedUserId;

final code = await engineProvider.connect(
ConnectParams(token: token),
handler: (userId, error) {
connectedUserId = userId;
callbackError = error;
},
);

if (code != 0 || (callbackError != null && !callbackError!.isSuccess)) {
throw StateError(
'Connection failed: ${callbackError?.message ?? callbackError?.code ?? code}',
);
}

debugPrint('Connected as $connectedUserId');

Before connecting, check that:

  • The app has fetched a fresh token from your own backend.
  • The token belongs to the same app and data center as InitParams.
  • Network access is available and the app has the required platform network permission.
  • Your UI handles the non-zero connect return code and the callback NCError.

Monitor connection state

Listen to the notifier for side effects.

Dart
engineProvider.connectionStatusNotifier.addListener(() {
final status = engineProvider.connectionStatus;
debugPrint('Connection status: $status');
});

Or rebuild UI from the current state.

Dart
final status = context.watch<EngineProvider>().connectionStatus;

Receive message events

ChatPage updates its own message list. Use an async listener when your app needs additional side effects, such as analytics, custom alerts, or badge synchronization.

Dart
engineProvider.addAsyncMessageReceivedListener((message) async {
debugPrint('Received message: ${message.messageId}');
});

Remove listeners when the owning object is disposed.

Disconnect

Dart
await engineProvider.disconnect(disablePush: false);