getAllSavedVehicles function

Future<List<NearbyDevice>> getAllSavedVehicles()

Retrieves all previously saved vehicles from native platform storage.

This method fetches the complete list of Bluetooth devices that have been saved as vehicles by the user. These devices are used for automatic trip detection and provide vehicle management capabilities.

Returns:

  • Future<List<NearbyDevice>>: List of saved vehicle devices
    • Returns empty list if no vehicles have been saved
    • Each device contains name, MAC address, RSSI, and other Bluetooth info

Data Processing:

  1. Retrieves JSON string from native platform
  2. Handles null/empty responses gracefully
  3. Decodes JSON array into NearbyDevice objects
  4. Provides debug logging for troubleshooting

Usage Example:

final savedVehicles = await getAllSavedVehicles();
if (savedVehicles.isNotEmpty) {
  print('Found ${savedVehicles.length} saved vehicles:');
  for (final vehicle in savedVehicles) {
    print('- ${vehicle.name} (${vehicle.address})');
  }
} else {
  print('No vehicles saved yet');
}

Platform Communication:

  • Method Channel: 'getAllSavedVehicles'
  • Return Format: JSON array string of device objects
  • Error Handling: Returns empty list for null/empty responses

Debug Information:

  • Logs raw JSON response from platform
  • Logs decoded vehicle list for verification
  • Helps troubleshoot vehicle detection issues

Use Cases:

  • Vehicle management UI display
  • Checking if specific vehicles are already saved
  • Validating trip detection configuration
  • Debugging automatic trip detection issues

Implementation

Future<List<NearbyDevice>> getAllSavedVehicles() async {
  String? result = await platform.invokeMethod('getAllSavedVehicles');
  if (result == null || result.isEmpty) {
    return [];
  }
  if (kDebugMode) {
    print("Got vehicle: $result");
  }
  var decodedList = jsonDecode(result);
  if (kDebugMode) {
    print("decoded Vehicle: $decodedList");
  }

  List<NearbyDevice> vehicleList = List<NearbyDevice>.from(
    decodedList.map((model) {
      NearbyDevice vehicle = NearbyDevice.fromJson(model);
      return vehicle;
    }),
  );

  if (kDebugMode) {
    print("-> Vehicle: $vehicleList");
  }

  return vehicleList;
}