Skip to content

Commit f35bb68

Browse files
committed
product overview issue fixed
1 parent 3325d11 commit f35bb68

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

ShopApp/.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Flutter",
9+
"request": "launch",
10+
"type": "dart"
11+
}
12+
]
13+
}

ShopApp/lib/main.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ class MyApp extends StatelessWidget {
2222
Widget build(BuildContext context) {
2323
return MultiProvider(
2424
providers: [
25-
ChangeNotifierProvider(create: (ctx) => Products()),
25+
ChangeNotifierProvider(create: (ctx) => Auth()),
26+
ChangeNotifierProxyProvider<Auth, Products>(
27+
create: null,
28+
update: (ctx, auth, previousProducts) => Products(
29+
auth.token,
30+
previousProducts == null ? [] : previousProducts.items,
31+
),
32+
),
2633
ChangeNotifierProvider(create: (ctx) => Cart()),
2734
ChangeNotifierProvider(create: (ctx) => Order()),
28-
ChangeNotifierProvider(create: (ctx) => Auth()),
2935
],
3036
child: Consumer<Auth>(
3137
builder: (ctx, authData, _) => MaterialApp(

ShopApp/lib/providers/products.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import 'package:ShopApp/models/httpException.dart';
2+
import 'package:ShopApp/providers/auth.dart';
23
import 'package:ShopApp/providers/product.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter/widgets.dart';
56
import 'package:http/http.dart' as http;
67
import 'dart:convert';
78

9+
import 'package:provider/provider.dart';
10+
811
class Products with ChangeNotifier {
912
List<Product> _list = [
1013
// Product(
@@ -39,7 +42,11 @@ class Products with ChangeNotifier {
3942
// imageUrl:
4043
// 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Cast-Iron-Pan.jpg/1024px-Cast-Iron-Pan.jpg',
4144
// ),
42-
];
45+
];
46+
47+
final authToken;
48+
49+
Products(this.authToken, this._list);
4350

4451
List<Product> get favItems {
4552
return _list.where((element) => element.isFavorite).toList();
@@ -54,7 +61,7 @@ class Products with ChangeNotifier {
5461
}
5562

5663
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';
5865
try {
5966
final response = await http.get(url);
6067
List<Product> tempList = [];
@@ -65,20 +72,21 @@ class Products with ChangeNotifier {
6572
description: value['description'],
6673
id: key,
6774
title: value['title'],
68-
price: value['price'],
75+
price: double.parse(value['price'].toString()),
6976
imageUrl: value['imageUrl'],
7077
isFavorite: value['isFavorite'],
7178
));
7279
});
7380
notifyListeners();
7481
_list = tempList;
7582
} catch (error) {
83+
print(error);
7684
throw error;
7785
}
7886
}
7987

8088
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';
8290
try {
8391
final response = await http.post(url,
8492
body: json.encode({
@@ -108,7 +116,7 @@ class Products with ChangeNotifier {
108116
int index = _list.indexWhere((element) => element.id == id);
109117
if (index >= 0) {
110118
final url =
111-
'https://fluttershopapp-f979d.firebaseio.com/products/$id.json';
119+
'https://fluttershopapp-f979d.firebaseio.com/products/$id.json?auth=$authToken';
112120
await http.patch(url,
113121
body: json.encode({
114122
'title': newProduct.title,
@@ -122,7 +130,7 @@ class Products with ChangeNotifier {
122130
}
123131

124132
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';
126134
int elementIndex = _list.indexWhere((element) => element.id == id);
127135
Product elementToBeDeleted = _list.elementAt(elementIndex);
128136
_list.removeWhere((element) => element.id == id);

0 commit comments

Comments
 (0)