portfolioPositions method

Future<List<PortfolioPosition>> portfolioPositions({
  1. bool includeCash = false,
})

Gets the portfolio positions.

PortfolioPosition contains the ProductInfo property with product infos.

includeCash: if true, the method adds the account cash position to the result.

Implementation

Future<List<PortfolioPosition>> portfolioPositions({
  bool includeCash = false,
}) async {
  final result = await _repository.getPortfolioPositionsRequest(
    sessionId,
    accountInfo.intAccount,
  );

  List<PortfolioPosition> positions = [];

  // Gets portfolio positions,
  // which only contain an id as product reference
  await result.when(
    (data) async {
      try {
        positions = processPortfolio(data, includeCash);
      } on Exception catch (e) {
        throw DegiroApiError(
          message: 'Something went wrong during portfolio processing',
          exception: e,
        )..methodName = 'processPortfolio';
      }

      // Gets product infos by ids
      final Set<String> productIds = positions.map((p) => p.id).toSet();
      final productInfos = await this.productInfos(productIds.toList());
      positions = positions
          .map(
            (position) => position.copyWith(
              productInfo: productInfos.firstWhere(
                (info) => info.id == position.id,
              ),
            ),
          )
          .toList();
    },
    (error) => throw error..methodName = 'portfolioPositions',
  );

  return positions;
}