switchActiveNetwork method
Switches an EVM network using EIP-3326. Error 4902 becomes NetworkNotAddedError; add the network and retry. Other chains are unsupported.
Implementation
@override
Future<void> switchActiveNetwork(String chain, String accountAddress, String networkId) async {
Future<void> instrumentationOperation() async {
final operationScope = _state.flowScope.capture(replace: false);
await operationScope.ready();
final walletConnectProjectId = _state.projectSettings?.sdk?.walletConnect?.projectId;
if (walletConnectProjectId == null) {
throw DynamicStateException("This environment has no WalletConnect projectId — enable WalletConnect for it in the Dynamic dashboard");
}
operationScope.check();
await _externalWalletProvider.initialize(walletConnectProjectId, _config.appName, _config.appLogoUrl, _config.nativeLink, _config.universalLink);
operationScope.check();
if (chain != "EVM") {
throw DynamicStateException("this wallet provider does not support switching networks for this chain");
}
_state.externalWalletActionRequested.emit(ExternalWalletActionRequest(action: "switchActiveNetwork", walletName: (_externalWalletState.externalWalletAccounts == null ? null : firstWhereOrNull<ExternalWalletAccount>(_externalWalletState.externalWalletAccounts!, (item) => item.address == accountAddress))?.displayName, address: accountAddress, chain: chain));
operationScope.check();
await _externalWalletProvider.switchActiveNetwork(chain, accountAddress, networkId);
operationScope.check();
}
return await _api.instrumentation.runAsync("externalWallet.switchActiveNetwork", instrumentationOperation);
}