api calling
unknown
dart
2 years ago
940 B
5
Indexable
// 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');
}
}
}
Editor is loading...
Leave a Comment