import 'dart:convert'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:primer_practica/src/models/articles_model.dart'; import '../http_api/articles_api.dart'; class ArticleController { final Connectivity _connectivity = Connectivity(); final ArticlesApi _articleApi = ArticlesApi(); Future> getArticles(int categoryId) async { Map mapResp = { 'ok': false, 'message': 'No hay artículos', 'data': null }; ConnectivityResult connectivityResult = await _connectivity.checkConnectivity(); if (connectivityResult != ConnectivityResult.none) { if (connectivityResult == ConnectivityResult.wifi || connectivityResult == ConnectivityResult.mobile) { Map respGet = await _articleApi.getArticles(); if (respGet['statusCode'] == 200) { try { var decodeResp = json.decode(respGet['body']); List listArticles = ArticlesModel.fromJsonArray(decodeResp['data']); mapResp['ok'] = true; mapResp['message'] = "${listArticles.length} artículos encontrados"; mapResp['data'] = listArticles; } catch (e) { mapResp['message'] = "Error en el procesamiento de datos: $e"; } } else { mapResp['message'] = "${respGet['body']}"; } } } return mapResp; } }