init method
Initialize the service: cache logged-in user, register SDK listeners, and initialize the Calls SDK. Safe to call multiple times — will no-op if already initialized.
Implementation
Future<void> init({CallingConfiguration? configuration}) async {
if (_initialized) return;
_configuration = configuration;
_loggedInUser = await CometChatUIKit.getLoggedInUser();
assert(
_loggedInUser != null,
'CallEventService.init(): No logged-in user. '
'Call CometChatUIKit.login() before initializing CallEventService.',
);
// Initialize the Calls SDK and await completion
final settings = CometChatUIKit.authenticationSettings;
assert(
settings?.appId != null && settings?.region != null,
'CallEventService.init(): CometChat must be initialized first. '
'authenticationSettings is null.',
);
if (settings != null &&
settings.appId != null &&
settings.region != null) {
await _initCallsSdk(settings.appId!, settings.region!);
}
// Log the user into the Calls SDK. CometChatCalls.init() only
// initializes the SDK — it does NOT authenticate the user. Without
// this login step, CometChatCalls.generateToken() fails with
// "User auth token is null" because the Calls SDK's internal
// CurrentUserRepository has no user record.
_callsSdkLoginCompleter = Completer<void>();
await _loginCallsSdk();
// Only complete the login completer if login succeeded.
// If it failed after all retries, leave it uncompleted so
// waitForCallsSdk() will time out rather than proceeding with a broken SDK.
if (_callsSdkLoginReady && !_callsSdkLoginCompleter!.isCompleted) {
_callsSdkLoginCompleter!.complete();
} else if (!_callsSdkLoginReady && !_callsSdkLoginCompleter!.isCompleted) {
// Login failed — complete anyway so we don't hang forever,
// but log a warning.
developer.log('CallEventService: WARNING — Calls SDK login failed, generateToken will fail');
_callsSdkLoginCompleter!.complete();
}
// Pre-cache the auth token so CallLogsBloc doesn't need to fetch it
try {
_cachedAuthToken = await CometChat.getUserAuthToken();
} catch (e) {
developer.log('CallEventService: getUserAuthToken failed: $e');
}
CometChat.addCallListener(_listenerId, this);
CometChatCallEvents.addCallEventsListener(_listenerId, this);
_initialized = true;
developer.log('CallEventService initialized');
}