ArticleApi.dart 977 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/foundation.dart';
import 'package:miapp_flutter/environments/archivo.dart';

class ArticleApi {
  final String apiUrl = 'articulo';

  // Método para obtener los artículos
  Future<Map<String, dynamic>> getArticles(int categoryId) async {
    String url = '${apiApp}/$apiUrl?categoria=$categoryId&offset=0&max=100';
    if (kDebugMode) {
      print('Url -> $url');
    }
    try {
      final response = await http.get(Uri.parse(url));

      // Verifica el estado de la respuesta
      if (response.statusCode == 200) {
        return {
          'statusCode': response.statusCode,
          'body': response.body,
        };
      } else {
        return {
          'statusCode': response.statusCode,
          'body': 'Error: ${response.reasonPhrase}',
        };
      }
    } catch (e) {
      return {
        'statusCode': 501,
        'body': 'Error: $e',
      };
    }
  }
}