getOngoingTrip method

Future<CurrentTripDataResponse?> getOngoingTrip()

Retrieves data for the currently active trip.

Returns real-time information about an ongoing trip including distance, duration, current location, and driving behavior metrics.

Returns:

  • CurrentTripDataResponse?: Current trip data, or null if no active trip

Usage:

try {
  final currentTrip = await communicator.getCurrentTrip();
  if (currentTrip != null) {
    print('Distance: ${currentTrip.distance} km');
    print('Duration: ${currentTrip.duration} minutes');
    print('Current speed: ${currentTrip.currentSpeed} km/h');
  } else {
    print('No active trip');
  }
} catch (e) {
  print('Failed to get current trip data: $e');
}

Throws:

  • Future.error("Unable to get current trip data"): When retrieval fails

Use Cases:

  • Real-time trip monitoring dashboards
  • Live trip statistics display
  • Progress tracking during ongoing trips

Implementation

Future<CurrentTripDataResponse?> getOngoingTrip() async {
  try {
    CurrentTripDataResponse? currentTripDataResponse = await kruzr_comm
        .getOngoingTrip();
    return currentTripDataResponse;
  } on PlatformException catch (e, stackTrace) {
    if (kDebugMode) {
      print("Error in getCurrentTrip");
      print(stackTrace);
      print(e);
    }
    return Future.error({"code": e.code, "message": e.message, "details": e.details});
  } on Exception catch (e, stackTrace) {
    if (kDebugMode) {
      print("Error in getCurrentTrip");
      print(stackTrace);
      print(e);
    }
    return Future.error(PlatformException(code: "PLUGIN_ERROR", message: e.toString()));
  }
}