Overview
The Reserva class represents a restaurant table reservation in the system. It contains information about the table, date/time, number of guests, and reservation status.
EstadoReserva Enum
Represents the possible states of a reservation:
enum EstadoReserva {
pendiente,
confirmada,
cancelada,
}
Initial state when a reservation is created
State after the reservation has been confirmed
State when the reservation has been cancelled
Class Definition
class Reserva {
final String id;
final String mesaId;
final DateTime fechaHora;
final int numeroPersonas;
final int duracionMinutos;
EstadoReserva estado;
final String? contactoCliente;
final String? nombreCliente;
final String? telefonoCliente;
final String? negocioId;
}
Properties
Unique identifier for the reservation
Identifier of the table being reserved
Date and time when the reservation starts
Number of people for the reservation
Duration of the reservation in minutes
estado
EstadoReserva
default:"EstadoReserva.pendiente"
Current status of the reservation (pendiente, confirmada, or cancelada)
Customer’s email for notifications (optional)
Customer’s name (optional)
Customer’s verified phone number (optional)
Identifier of the restaurant where the reservation was made (optional)
Constructor
Reserva({
required this.id,
required this.mesaId,
required this.fechaHora,
required this.numeroPersonas,
this.duracionMinutos = 60,
this.estado = EstadoReserva.pendiente,
this.contactoCliente,
this.nombreCliente,
this.telefonoCliente,
this.negocioId,
})
Methods
horaFin
Computed property that calculates the end time of the reservation.
DateTime get horaFin => fechaHora.add(Duration(minutes: duracionMinutos));
Returns: DateTime - The calculated end time of the reservation
confirmar()
Confirms a pending reservation. Updates the reservation status to confirmada.
Throws:
Exception - If attempting to confirm a cancelled reservation
Exception - If the reservation is already confirmed
Example:
final reserva = Reserva(
id: '1',
mesaId: 'mesa-01',
fechaHora: DateTime(2026, 3, 15, 20, 0),
numeroPersonas: 4,
);
reserva.confirmar(); // Changes estado to EstadoReserva.confirmada
copyWith()
Creates a copy of the reservation with specified fields updated.
Reserva copyWith({
String? id,
String? mesaId,
DateTime? fechaHora,
int? numeroPersonas,
int? duracionMinutos,
EstadoReserva? estado,
String? contactoCliente,
String? nombreCliente,
String? telefonoCliente,
String? negocioId,
})
Returns: Reserva - A new instance with updated fields
Example:
final reservaOriginal = Reserva(
id: '1',
mesaId: 'mesa-01',
fechaHora: DateTime(2026, 3, 15, 20, 0),
numeroPersonas: 4,
);
final reservaModificada = reservaOriginal.copyWith(
numeroPersonas: 6,
duracionMinutos: 90,
);
Usage Examples
Creating a New Reservation
final reserva = Reserva(
id: 'res-123',
mesaId: 'mesa-05',
fechaHora: DateTime(2026, 3, 20, 19, 30),
numeroPersonas: 2,
duracionMinutos: 90,
contactoCliente: '[email protected]',
nombreCliente: 'Juan Pérez',
telefonoCliente: '+34612345678',
negocioId: 'rest-001',
);
Confirming a Reservation
try {
reserva.confirmar();
print('Reserva confirmada exitosamente');
} catch (e) {
print('Error al confirmar: $e');
}
Calculating End Time
final reserva = Reserva(
id: '1',
mesaId: 'mesa-01',
fechaHora: DateTime(2026, 3, 15, 20, 0),
numeroPersonas: 4,
duracionMinutos: 120,
);
print('Hora de inicio: ${reserva.fechaHora}');
print('Hora de fin: ${reserva.horaFin}'); // 22:00