fundamentals method

  1. @override
Future<Fundamentals> fundamentals(
  1. SecurityIdentifier id
)
override

The fundamentals method is used to get the fundamentals for a given stock symbol.

The id argument is used to specify the stock id.

// Get fundamentals for Apple
final client = BavestRestClient(api_key);
final fundamentals = client.fundamentals(SecurityIdentifier(symbol: "AAPL"));

Implementation

@override
Future<Fundamentals> fundamentals(SecurityIdentifier id) async {
  const url = '$_baseUrl/stock/fundamentals';
  final params = id.toJson();

  var response = await _post(url, params);
  if (response != null && response.statusCode == 200) {
    var fundamentals = Fundamentals.fromJson(jsonDecode(response.data));
    final seen = <String>{};
    if (fundamentals.metrics == null) return fundamentals;

    fundamentals.metrics!.removeWhere((e) {
      var isDuplicate = seen.contains(e.period.toString());
      seen.add(e.period.toString());

      return isDuplicate;
    });

    if (fundamentals.metrics == null) return fundamentals;

    fundamentals.metrics!
        .sort((a, b) => b.period!.year.compareTo(a.period!.year));

    return fundamentals;
  }

  throw Exception("could not fetch fundamentals for $id");
}