1
1
import 'package:ShopApp/models/httpException.dart' ;
2
+ import 'package:ShopApp/providers/auth.dart' ;
2
3
import 'package:ShopApp/providers/product.dart' ;
3
4
import 'package:flutter/material.dart' ;
4
5
import 'package:flutter/widgets.dart' ;
5
6
import 'package:http/http.dart' as http;
6
7
import 'dart:convert' ;
7
8
9
+ import 'package:provider/provider.dart' ;
10
+
8
11
class Products with ChangeNotifier {
9
12
List <Product > _list = [
10
13
// Product(
@@ -39,7 +42,11 @@ class Products with ChangeNotifier {
39
42
// imageUrl:
40
43
// 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg',
41
44
// ),
42
- ];
45
+ ];
46
+
47
+ final authToken;
48
+
49
+ Products (this .authToken, this ._list);
43
50
44
51
List <Product > get favItems {
45
52
return _list.where ((element) => element.isFavorite).toList ();
@@ -54,7 +61,7 @@ class Products with ChangeNotifier {
54
61
}
55
62
56
63
Future <void > fetchProducts () async {
57
- const url = 'https://fluttershopapp-f979d.firebaseio.com/products.json' ;
64
+ final url = 'https://fluttershopapp-f979d.firebaseio.com/products.json?auth=$ authToken ' ;
58
65
try {
59
66
final response = await http.get (url);
60
67
List <Product > tempList = [];
@@ -65,20 +72,21 @@ class Products with ChangeNotifier {
65
72
description: value['description' ],
66
73
id: key,
67
74
title: value['title' ],
68
- price: value['price' ],
75
+ price: double . parse ( value['price' ]. toString ()) ,
69
76
imageUrl: value['imageUrl' ],
70
77
isFavorite: value['isFavorite' ],
71
78
));
72
79
});
73
80
notifyListeners ();
74
81
_list = tempList;
75
82
} catch (error) {
83
+ print (error);
76
84
throw error;
77
85
}
78
86
}
79
87
80
88
Future <void > addProduct (Product product) async {
81
- const url = 'https://fluttershopapp-f979d.firebaseio.com/products.json' ;
89
+ const url = 'https://fluttershopapp-f979d.firebaseio.com/products.json?auth=$ authToken ' ;
82
90
try {
83
91
final response = await http.post (url,
84
92
body: json.encode ({
@@ -108,7 +116,7 @@ class Products with ChangeNotifier {
108
116
int index = _list.indexWhere ((element) => element.id == id);
109
117
if (index >= 0 ) {
110
118
final url =
111
- 'https://fluttershopapp-f979d.firebaseio.com/products/$id .json' ;
119
+ 'https://fluttershopapp-f979d.firebaseio.com/products/$id .json?auth=$ authToken ' ;
112
120
await http.patch (url,
113
121
body: json.encode ({
114
122
'title' : newProduct.title,
@@ -122,7 +130,7 @@ class Products with ChangeNotifier {
122
130
}
123
131
124
132
Future <void > deleteProduct (String id) async {
125
- final url = 'https://fluttershopapp-f979d.firebaseio.com/products/$id .json' ;
133
+ final url = 'https://fluttershopapp-f979d.firebaseio.com/products/$id .json?auth=$ authToken ' ;
126
134
int elementIndex = _list.indexWhere ((element) => element.id == id);
127
135
Product elementToBeDeleted = _list.elementAt (elementIndex);
128
136
_list.removeWhere ((element) => element.id == id);
0 commit comments