selectChain method
Future<void>
selectChain(
- ReownAppKitModalNetworkInfo? chainInfo, {
- bool switchChain = false,
- bool logEvent = true,
override
Sets the selectedChain
If the wallet is already connected, it will request the chain to be changed and will update the session with the new chain.
If chainInfo
is null this will disconnect the wallet.
Implementation
@override
Future<void> selectChain(
ReownAppKitModalNetworkInfo? chainInfo, {
bool switchChain = false,
bool logEvent = true,
}) async {
_checkInitialized();
if (chainInfo?.chainId == _selectedChainID) {
return;
}
// If the chain is null, disconnect and stop.
if (chainInfo == null) {
await disconnect();
return;
}
_chainBalance = null;
try {
final formattedBalance = CoreUtils.formatChainBalance(_chainBalance);
balanceNotifier.value = '$formattedBalance ${chainInfo.currency}';
final hasValidSession = _isConnected && _currentSession != null;
final ns = NamespaceUtils.getNamespaceFromChain(chainInfo.chainId);
final approvedChains = _currentSession?.getApprovedChains(namespace: ns);
final isApproved = (approvedChains ?? []).contains(chainInfo.chainId);
if (switchChain &&
hasValidSession &&
_selectedChainID != null &&
!isApproved) {
await requestSwitchToChain(chainInfo);
} else {
await _setLocalEthChain(chainInfo.chainId, logEvent: logEvent);
}
} on JsonRpcError catch (e) {
onModalError.broadcast(ModalError(e.message ?? 'An error occurred'));
} on ReownAppKitModalException catch (e) {
onModalError.broadcast(ModalError(e.message));
} catch (e, s) {
_appKit.core.logger.e('[$runtimeType] selectChain, error: $e, $s');
onModalError.broadcast(ModalError('An error occurred'));
}
}