removeWallet method

Future<void> removeWallet(
  1. String walletId
)

Remove wallet - persists on disk

Implementation

Future<void> removeWallet(String walletId) async {
  final wallet = _wallets.firstWhereOrNull((w) => w.id == walletId);
  if (wallet != null) {
    // Dispose with provider
    final provider = _providers[wallet.type];
    if (provider != null) {
      await provider.removeWallet(wallet);
    }
  }

  await _repository.removeWallet(walletId);

  // clean up in-memory data
  _wallets.removeWhere((wallet) => wallet.id == walletId);
  _walletsBalances.remove(walletId);
  _walletsPendingTransactions.remove(walletId);
  _walletsRecentTransactions.remove(walletId);

  // clean up streams
  _walletBalanceStreams[walletId]?.close();
  _walletPendingTransactionStreams[walletId]?.close();
  _walletRecentTransactionStreams[walletId]?.close();

  _walletBalanceStreams.remove(walletId);
  _walletPendingTransactionStreams.remove(walletId);
  _walletRecentTransactionStreams.remove(walletId);

  // clean up subscriptions
  _subscriptions[walletId]?.forEach((sub) => sub.cancel());
  _subscriptions.remove(walletId);

  // update wallets stream with the new list
  _safeAddWallets(_wallets.toList());

  _updateCombinedStreams();

  if (walletId == _repository.getDefaultWalletIdForReceiving()) {
    Wallet? wallet = _wallets.where((w) => w.canReceive).firstOrNull;
    if (wallet != null) {
      _repository.setDefaultWalletForReceiving(wallet.id);
    }
  }
  if (walletId == _repository.getDefaultWalletIdForSending()) {
    Wallet? wallet = _wallets.where((w) => w.canSend).firstOrNull;
    if (wallet != null) {
      _repository.setDefaultWalletForSending(wallet.id);
    }
  }
}