import '../enums/vehicle_command_type.dart'; class VehicleCommand { final VehicleCommandType type; final Map? params; final String error; VehicleCommand({required this.type, this.params, this.error = ''}); // 从字符串创建命令(用于从API响应解析) factory VehicleCommand.fromString( String commandStr, Map? params, String error) { // 将字符串转换为枚举值 VehicleCommandType type; try { type = VehicleCommandType.values.firstWhere( (e) => e.name == commandStr, orElse: () => VehicleCommandType.unknown, ); } catch (e) { print('Error parsing command string: $e'); // 默认为搜车指令,或者你可以选择抛出异常 type = VehicleCommandType.unknown; } return VehicleCommand(type: type, params: params, error: error); } // 将命令转换为字符串(用于API请求) String get commandString => type.name; @override String toString() { return 'VehicleCommand(command: ${type.name}, params: $params)'; } }