- Fix RangeError due to changes in _tradeAvailableProviders while the Futures complete

- Silently catch "Concurrent modification during iteration"
This commit is contained in:
OmarHatem 2023-02-07 23:48:10 +02:00
parent 74b571fc77
commit e67b484613

View file

@ -308,10 +308,11 @@ abstract class ExchangeViewModelBase with Store {
Future<void> _calculateBestRate() async { Future<void> _calculateBestRate() async {
final amount = double.tryParse(isFixedRateMode ? receiveAmount : depositAmount) ?? 1; final amount = double.tryParse(isFixedRateMode ? receiveAmount : depositAmount) ?? 1;
final _providers = _tradeAvailableProviders
.where((element) => !isFixedRateMode || element.supportsFixedRate).toList();
final result = await Future.wait<double>( final result = await Future.wait<double>(
_tradeAvailableProviders _providers.map((element) => element.fetchRate(
.where((element) => !isFixedRateMode || element.supportsFixedRate)
.map((element) => element.fetchRate(
from: depositCurrency, from: depositCurrency,
to: receiveCurrency, to: receiveCurrency,
amount: amount, amount: amount,
@ -324,7 +325,12 @@ abstract class ExchangeViewModelBase with Store {
for (int i=0;i<result.length;i++) { for (int i=0;i<result.length;i++) {
if (result[i] != 0) { if (result[i] != 0) {
/// add this provider as its valid for this trade /// add this provider as its valid for this trade
_sortedAvailableProviders[result[i]] = _tradeAvailableProviders[i]; try {
_sortedAvailableProviders[result[i]] = _providers[i];
} catch (e) {
// will throw "Concurrent modification during iteration" error if modified at the same
// time [createTrade] is called, as this is not a normal map, but a sorted map
}
} }
} }
if (_sortedAvailableProviders.isNotEmpty) { if (_sortedAvailableProviders.isNotEmpty) {