api_rest_flutter_web 3.0.0
api_rest_flutter_web: ^3.0.0 copied to clipboard
Flutter package for managing data operations with cloud backends. Provides a unified API layer for CRUD operations, data validation, schema management, and query building. Web-only support.
example/example.dart
// ignore_for_file: unused_local_variable, unused_import
import 'dart:convert';
import 'package:api_rest_flutter_web/api_rest.dart';
import 'package:api_rest_flutter_web/util/utils_data_api_rest.dart';
import 'package:flutter_models_provider/global/environment.dart';
import 'package:flutter_models_provider/model/objectbox/coleccion_objectbox_model.dart';
/// Example demonstrating basic usage of api_rest_flutter_web
///
/// Note: This example shows the API patterns. Actual execution requires
/// proper initialization of CloudDb and GestorData instances.
void main() async {
// Initialize ApiRest (requires actual CloudDb and GestorData instances)
// final apiRest = ApiRest(
// cloudDb: yourCloudDbInstance,
// modeToWork: ModeToWork.online,
// gestorData: yourGestorDataInstance,
// coleccionFuncionesBackend: 'funcionesBackend',
// enableDebugLogs: true,
// );
// await apiRest.init();
}
/// Example: Query multiple records with pagination
///
/// ```dart
/// final response = await apiRest.obtener(
/// coleccion: 'localidad',
/// consulta: '{"data.idProvincia": "631f7cfbafbdb0417d7bcf8b"}',
/// argsLocalBD: [['idProvincia'], ['='], ['631f7cfbafbdb0417d7bcf8b']],
/// limit: '25',
/// skip: '0',
/// ordenar: '{"data.descripcion": 1}',
/// );
///
/// if (response.containsKey(EnvironmentApiRest.dataOk)) {
/// final List items = response[EnvironmentApiRest.data];
/// final int totalItems = response[EnvironmentApiRest.totalItems];
/// }
/// ```
void exampleObtener() {}
/// Example: Read a single record by query
///
/// ```dart
/// final response = await apiRest.leeById(
/// coleccion: 'ciudadano',
/// consulta: '{"idColeccion": "631f8b62d34bdc52b333879b"}',
/// argsLocalBD: [['idServer'], ['='], ['631f8b62d34bdc52b333879b']],
/// );
///
/// if (response.containsKey(EnvironmentApiRest.dataOk)) {
/// final ColeccionObjBox record = response[EnvironmentApiRest.data];
/// print('Found record: ${record.idServer}');
/// }
/// ```
void exampleLeeById() {}
/// Example: Save a new record
///
/// ```dart
/// // Create record using ColeccionObjBox constructor or factory
/// final newRecord = ColeccionObjBox(
/// coleccion: 'ciudadano',
/// data: jsonEncode({
/// 'nombre': 'Juan',
/// 'apellido': 'Perez',
/// 'email': 'juanperez@example.com',
/// }),
/// );
///
/// final response = await apiRest.guardar(
/// data: newRecord,
/// versionData: '1',
/// );
///
/// if (response.containsKey(EnvironmentApiRest.dataOk)) {
/// final savedRecord = response[EnvironmentApiRest.data];
/// print('Saved with ID: ${savedRecord.idServer}');
/// }
/// ```
void exampleGuardar() {}
/// Example: Update an existing record
///
/// The package automatically detects changes between `data` and `dataOriginal`
/// and only sends modified fields to the server.
///
/// ```dart
/// // existingRecord has both data (current) and dataOriginal (from server)
/// final response = await apiRest.actualizar(
/// data: existingRecord,
/// versionData: '1',
/// );
///
/// if (response.containsKey(EnvironmentApiRest.dataOk)) {
/// if (response[EnvironmentApiRest.msgStatus] ==
/// EnvironmentApiRest.literalDataSinModificar) {
/// print('No changes detected');
/// } else {
/// print('Record updated successfully');
/// }
/// }
/// ```
void exampleActualizar() {}
/// Example: Change record state
///
/// ```dart
/// final response = await apiRest.cambiaEstado(
/// coleccion: 'ciudadano',
/// id: '631f8b62d34bdc52b333879b',
/// estado: 'A', // Active
/// );
/// ```
void exampleCambiaEstado() {}
/// Example: Validate data against a schema
///
/// ```dart
/// final utilsData = UtilsDataApiRest(apiRest: apiRest);
///
/// final esquema = {
/// 'type': 'object',
/// 'properties': {
/// 'nombre': {'type': 'string', 'requerido': true, 'label': 'Nombre'},
/// 'email': {'type': 'string', 'requerido': true, 'tipoDato': 'correo'},
/// 'edad': {'type': 'number', 'tipoDato': 'entero'},
/// },
/// };
///
/// final data = {'nombre': 'Juan', 'email': 'juan@example.com', 'edad': 30};
///
/// final result = await utilsData.validaDataInMap(
/// esquema: esquema,
/// mapaActualizado: data,
/// );
///
/// if (result.containsKey(EnvironmentApiRest.dataOk)) {
/// print('Validation passed!');
/// } else {
/// print('Error: ${result[EnvironmentApiRest.error]}');
/// }
/// ```
void exampleValidation() {}