init method
Sets up the explorer and appKit if they already been initialized.
Implementation
@override
Future<void> init() async {
_relayConnected = false;
_awaitRelayOnce = Completer<bool>();
if (!CoreUtils.isValidProjectID(_projectId)) {
throw 'Please provide a valid projectId ($_projectId).'
'See ${UrlConstants.docsUrl}/appkit/flutter/core/usage for details.';
}
if (_status == ReownAppKitModalStatus.initializing ||
_status == ReownAppKitModalStatus.initialized) {
return;
}
_status = ReownAppKitModalStatus.initializing;
_notify();
_registerListeners();
await _appKit.init();
await _networkService.init();
await _explorerService.init();
await _coinbaseService.init();
await _blockchainService.init();
_currentSession = await _getStoredSession();
_currentSelectedChainId = _getStoredChainId(null);
final isMagic = _currentSession?.sessionService.isMagic == true;
final isCoinbase = _currentSession?.sessionService.isCoinbase == true;
if (isMagic || isCoinbase) {
_currentSelectedChainId ??= _currentSession!.chainId;
await _setSesionAndChainData(_currentSession!);
if (isMagic) {
final caip2Chain = ReownAppKitModalNetworks.getCaip2Chain(
_currentSelectedChainId!,
);
await _magicService.init(chainId: caip2Chain);
}
} else {
_magicService.init();
}
await expirePreviousInactivePairings();
final wcPairings = _appKit.pairings.getAll();
final wcSessions = _appKit.sessions.getAll();
// Loop through all the chain data
final allNetworks = ReownAppKitModalNetworks.getAllSupportedNetworks();
for (final chain in allNetworks) {
final namespace = ReownAppKitModalNetworks.getNamespaceForChainId(
chain.chainId,
);
final requiredEvents = _requiredNamespaces[namespace]?.events ?? [];
final optionalEvents = _optionalNamespaces[namespace]?.events ?? [];
final events = [...requiredEvents, ...optionalEvents];
for (final event in events) {
_appKit.registerEventHandler(
chainId: '$namespace:${chain.chainId}',
event: event,
);
}
}
// There's a session stored
if (wcSessions.isNotEmpty) {
await _storeSession(ReownAppKitModalSession(
sessionData: wcSessions.first,
));
// session should not outlive the pairing
if (wcPairings.isEmpty) {
await disconnect();
} else {
await _checkSIWEStatus();
}
} else {
// Check for other type of sessions stored
if (_currentSession != null) {
if (_currentSession!.sessionService.isCoinbase) {
final isCbConnected = await _coinbaseService.isConnected();
if (!isCbConnected) {
await _cleanSession();
}
} else if (_currentSession!.sessionService.isMagic) {
// Every time the app gets killed Magic service will treat the user as disconnected
// So we will need to treat magic session differently
final email = _currentSession!.email;
_magicService.setEmail(email);
final provider = _currentSession!.socialProvider;
_magicService.setProvider(provider);
} else {
await _cleanSession();
}
}
}
// Get the chainId of the chain we are connected to.
await _selectChainFromStoredId();
onModalNetworkChange.subscribe(_onNetworkChainRequireSIWE);
onModalConnect.subscribe(_loadAccountData);
_relayConnected = _appKit.core.relayClient.isConnected;
if (!_relayConnected) {
_relayConnected = await _awaitRelayOnce.future;
} else {
if (!_awaitRelayOnce.isCompleted) {
_awaitRelayOnce.complete(_relayConnected);
}
}
_status = _relayConnected
? ReownAppKitModalStatus.initialized
: ReownAppKitModalStatus.initializing;
_appKit.core.logger.i('[$runtimeType] status $_status');
_notify();
}