getStatistics method

Future<List<Statistic>> getStatistics({
  1. required List<StatisticsType> ofTypes,
  2. required DateTime from,
  3. required DateTime to,
  4. required StatisticsInterval interval,
  5. StatisticsFilter? filter,
})

Retrieves all statistics based on the specified configuration.

ofTypes - Array of statistic types to retrieve (e.g., heart rate, steps, sleep)

from - Start datetime for the data range (inclusive)

to - End datetime for the data range (exclusive)

interval - Time interval for aggregating the data (daily or hourly)

filter - Optional filter to apply to the results (can filter by provider, etc.)

Implementation

Future<List<Statistic>> getStatistics({
  required List<StatisticsType> ofTypes,
  required DateTime from,
  required DateTime to,
  required StatisticsInterval interval,
  StatisticsFilter? filter,
}) async {
  final result = await NativeSDKBridgeV3.getStatistics(
    connectionId: connectionId,
    ofTypes: ofTypes.map((e) => e.toJson()).toList(),
    from: from.toUtc().toIso8601String(),
    to: to.toUtc().toIso8601String(),
    interval: interval.toJson(),
    filter: filter != null ? jsonEncode(filter.toJson()) : null,
  );
  // log("Statistics result: $result");

  ExceptionHandler.checkException(result);

  final object = jsonDecode(result) as List;
  List<Statistic> statistics = object.map((e) => Statistic.fromJson(e)).toList();

  return statistics;
}