CrudViewModel<T extends Entity> class
abstract
ViewModel para operaciones CRUD sobre entidades.
Responsabilidades:
- Ejecutar operaciones CRUD (Create, Read, Update, Delete, List)
- Comunicarse con Use Cases del dominio
- Gestionar el estado de la lista de entidades
NO es responsable de:
- Gestión de campos del formulario (eso es de EntityFormViewModel)
- Validaciones de UI (eso es de FormValidators)
- Coordinación entre ViewModels (eso es del Widget)
Tipo genérico:
T: Tipo de entidad del dominio
Ejemplo de implementación:
class PatientCrudViewModel with ChangeNotifier {
final CreatePatientUseCase _createUseCase;
final GetPatientUseCase _getUseCase;
final UpdatePatientUseCase _updateUseCase;
final DeletePatientUseCase _deleteUseCase;
final GetPatientsUseCase _getPatientsUseCase;
List<Patient> _patients = [];
List<Patient> get patients => _patients;
Future<OperationResult<Patient>> addEntity(Patient patient) async {
final result = await _createUseCase.call(patient);
return result.fold(
(error) => OperationResult.failure(error),
(success) {
getEntities(); // Recargar lista
return OperationResult.success(success);
},
);
}
Future<OperationResult<Patient>> getEntity(String id) async {
final result = await _getUseCase.call(id);
return result.fold(
(error) => OperationResult.failure(error),
(patient) => OperationResult.success(patient),
);
}
Future<OperationResult<Patient>> updateEntity(Patient patient) async {
final result = await _updateUseCase.call(patient);
return result.fold(
(error) => OperationResult.failure(error),
(success) {
getEntities();
return OperationResult.success(success);
},
);
}
Future<OperationResult<void>> deleteEntity(String id) async {
final result = await _deleteUseCase.call(id);
return result.fold(
(error) => OperationResult.failure(error),
(success) {
getEntities();
return OperationResult.success(null);
},
);
}
Future<void> getEntities() async {
final result = await _getPatientsUseCase.call();
result.fold(
(error) => // Manejar error,
(patients) {
_patients = patients;
notifyListeners();
},
);
}
}
// En el Widget, coordinas ambos ViewModels:
ElevatedButton(
onPressed: () async {
final patient = formViewModel.mapDataToEntity();
if (patient != null) {
final result = await crudViewModel.addEntity(patient);
if (result.isSuccess) {
formViewModel.clearFormData();
}
}
},
child: Text('Guardar'),
)
- Mixed-in types
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addEntity(
T entity) → Future< OperationResult< T> > - Crea una nueva entidad en el dominio.
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
deleteEntity(
String id) → Future< OperationResult< void> > - Elimina una entidad del dominio por su ID.
-
dispose(
) → void -
Discards any resources used by the object.
inherited
-
getEntities(
) → Future< void> - Obtiene la lista de entidades del dominio.
-
getEntity(
String id) → Future< OperationResult< T> > - Lee una entidad del dominio por su ID.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
updateEntity(
T entity) → Future< OperationResult< T> > - Actualiza una entidad existente en el dominio.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited