api calling

mail@pastecode.io avatar
unknown
dart
2 months ago
940 B
1
Indexable
Never
// Define your model class
class Item {
  final String id;
  final String description;
  final List<String> images;

  Item({required this.id, required this.description, required this.images});

  factory Item.fromJson(Map<String, dynamic> json) {
    return Item(
      id: json['id'],
      description: json['description'],
      images: List<String>.from(json['images'].map((x) => x['link'])),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'description': description,
      'images': images.map((x) => {'link': x}).toList(),
    };
  }
}

// Controller class to handle API calls
class ApiController {
  Future<Item> fetchItem() async {
    final response = await http.get(Uri.parse('YOUR_API_URL'));
    if (response.statusCode == 200) {
      return Item.fromJson(json.decode(response.body));
    } else {
      throw Exception('Failed to load item');
    }
  }
}
Leave a Comment