init static method
Initialize the SDK.
Implementation
static Future<void> init({
String? publicKey,
String? baseUrl,
bool? enableLogs,
}) async {
final debugLogs = enableLogs ?? _debugLoggingEnabled;
final normalizedBaseUrl = normalizeBaseUrl(baseUrl ?? _defaultBaseUrl);
final trimmedKey = publicKey?.trim() ?? '';
_applyDebugLogging(debugLogs);
_config = MyAppCrewConfig(
publicKey: trimmedKey,
baseUrl: normalizedBaseUrl,
debugLogs: debugLogs,
timeout: _defaultTimeout,
flushAt: _defaultFlushAt,
flushInterval: _defaultFlushInterval,
forceRebootstrap: false,
);
_logger ??= MyAppCrewLogger(debugLogs);
_client ??= MyAppCrewClient(timeout: _defaultTimeout, logger: _logger!);
_storage ??= MyAppCrewStorage();
_testerIdentityStore ??= TesterIdentityStore();
_queue ??= MyAppCrewQueue();
_ensureSessionId();
_appContext = await _loadAppContext();
await _loadTesterIdentity(trimmedKey);
_isInitialized = false;
_isEnabled = false;
_lastError = null;
if (trimmedKey.isEmpty) {
_accessToken = null;
_testerIdentity = null;
_testerId = null;
_ingestUrl = null;
_lastError = 'missing_public_key';
_emitDisabledLogOnce();
_isInitialized = true;
_startObservers();
return;
}
_logger?.log('init start');
final persisted = await _storage?.loadAuth();
if (!_config!.forceRebootstrap && _isAuthReusable(persisted)) {
_accessToken = persisted?.accessToken;
if (!_isTesterConnected(_testerIdentity)) {
_testerId = persisted?.testerId;
} else {
_testerId = _testerIdentity?.testerId ?? _testerId;
}
_ingestUrl = persisted?.ingestUrl ?? _defaultIngestPath;
_authState =
_isConnectedTesterId(_testerId)
? AuthState.connected
: AuthState.disconnected;
_isInitialized = true;
_isEnabled = true;
_lastError = null;
_syncQueuedEventsWithTester();
_startObservers();
logEvent('app_open');
_logger?.log('init ok (cached)');
return;
}
if (_isConnectedTesterId(_testerIdentity?.testerId ?? _testerId)) {
_authState = AuthState.connectedAuthStale;
final ok = await _recoverAuthForConnectedTester();
_isInitialized = true;
_isEnabled = true;
_startObservers();
logEvent('app_open');
if (!ok) {
_lastError ??= 'auth_stale';
_logger?.log('init ok (auth stale)');
return;
}
_logger?.log('init ok (recovered)');
return;
}
final ok = await _bootstrap();
_isInitialized = true;
_isEnabled = ok;
_startObservers();
logEvent('app_open');
if (!ok) {
_lastError ??= 'bootstrap_failed';
_logger?.log('init failed');
return;
}
_logger?.log('init ok');
}