getWallets method

Future<List<WalletApp>> getWallets()

Implementation

Future<List<WalletApp>> getWallets() async {
  final currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  if (_cacheTtl != null &&
      _walletsListCacheCreationTimestamp != null &&
      currentTimestamp > _walletsListCacheCreationTimestamp! + _cacheTtl!) {
    _dynamicWalletsListCache = null;
  }

  if (_dynamicWalletsListCache == null) {
    List<WalletApp> walletsList = [];
    try {
      final response = await http.get(Uri.parse(_walletsListSource));
      if (response.statusCode == 200) {
        final responseBody = jsonDecode(response.body);
        if (responseBody is List) {
          walletsList =
              responseBody.map((e) => WalletApp.fromMap(e)).toList();
        } else {
          throw FetchWalletsError(
              'Wrong wallets list format, wallets list must be an array.');
        }
      } else {
        throw FetchWalletsError('Failed to fetch wallets list.');
      }
    } catch (e) {
      walletsList = fallbackWalletsList;
    }

    walletsListCache = [];
    for (final wallet in walletsList) {
      walletsListCache.add(wallet);
    }

    _walletsListCacheCreationTimestamp = currentTimestamp;
  }

  return walletsListCache;
}