-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct_provider.dart
49 lines (42 loc) · 1.57 KB
/
product_provider.dart
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
38
39
40
41
42
43
44
45
46
47
48
49
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:muhmad_omar_haj_hmdo/constants.dart';
import 'package:muhmad_omar_haj_hmdo/product_model.dart';
import 'package:http/http.dart' as http;
// this example for products
// {
// "id": 1,
// "title": "Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops",
// "price": 109.95,
// "description": "Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday",
// "category": "men's clothing",
// "image": "https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg",
// "rating": {
// "rate": 3.9,
// "count": 120
// }
// },
class ProductProvider extends ChangeNotifier {
List<Product> _products = [];
List<Product> get products => _products;
Future<void> fetchProducts() async {
String apiLink = API_LINK;
try {
print("-------------111----------------------------------");
final response = await http.get(Uri.parse(apiLink));
print("-------------2222----------------------------------");
if (response.statusCode == 200) {
final data = json.decode(response.body) as List;
_products = data.map((json) => Product.fromJson(json)).toList();
print(_products);
notifyListeners();
} else {
throw Exception("حدث خطأ أثناء تحميل المنتجات");
}
} on Exception catch (e) {
print(e);
_products = [];
notifyListeners();
}
}
}