- Fix RangeError due to changes in _tradeAvailableProviders while the Futures complete - Silently catch "Concurrent modification during iteration"

- Fix RangeError due to changes in _tradeAvailableProviders while the Futures complete - Silently catch "Concurrent modification during iteration"

OmarHatem committed Feb 7, 2023 at 23:48 UTC e67b484613445d7efb2d5dbcde05eefc956b5d0a
1 file changed +11 -5
lib/view_model/exchange/exchange_view_model.dart
+11 -5
@@ -192,7 +192,7 @@ abstract class ExchangeViewModelBase with Store {
192 ObservableList<ExchangeTemplate> get templates =>
193 _exchangeTemplateStore.templates;
194
195 -
195 +
196 @computed
197 TransactionPriority get transactionPriority {
198 final priority = _settingsStore.priority[wallet.type];
@@ -308,10 +308,11 @@ abstract class ExchangeViewModelBase with Store {
308 Future<void> _calculateBestRate() async {
309 final amount = double.tryParse(isFixedRateMode ? receiveAmount : depositAmount) ?? 1;
310
311 + final _providers = _tradeAvailableProviders
312 + .where((element) => !isFixedRateMode || element.supportsFixedRate).toList();
313 +
314 final result = await Future.wait<double>(
312 - _tradeAvailableProviders
313 - .where((element) => !isFixedRateMode || element.supportsFixedRate)
314 - .map((element) => element.fetchRate(
315 + _providers.map((element) => element.fetchRate(
316 from: depositCurrency,
317 to: receiveCurrency,
318 amount: amount,
@@ -324,7 +325,12 @@ abstract class ExchangeViewModelBase with Store {
325 for (int i=0;i<result.length;i++) {
326 if (result[i] != 0) {
327 /// add this provider as its valid for this trade
327 - _sortedAvailableProviders[result[i]] = _tradeAvailableProviders[i];
328 + try {
329 + _sortedAvailableProviders[result[i]] = _providers[i];
330 + } catch (e) {
331 + // will throw "Concurrent modification during iteration" error if modified at the same
332 + // time [createTrade] is called, as this is not a normal map, but a sorted map
333 + }
334 }
335 }
336 if (_sortedAvailableProviders.isNotEmpty) {