File tree Expand file tree Collapse file tree 2 files changed +39
-26
lines changed Expand file tree Collapse file tree 2 files changed +39
-26
lines changed Original file line number Diff line number Diff line change
1
+ import 'dart:html' ;
2
+
1
3
import 'package:flutter/foundation.dart' ;
2
4
import 'dart:convert' ;
3
5
import 'package:http/http.dart' as http;
@@ -73,8 +75,23 @@ class Products with ChangeNotifier {
73
75
}
74
76
}
75
77
76
- void updateProduct (String id, Product newProduct) {
78
+ Future < void > updateProduct (String id, Product newProduct) async {
77
79
final prodIndex = _items.indexWhere ((element) => element.id == id);
80
+ if (prodIndex > 0 ) {
81
+ final url =
82
+ 'https://flutter-shop-app-c87f7-default-rtdb.firebaseio.com/products/$id .json' ;
83
+ await http.patch (
84
+ Uri .parse (url),
85
+ body: json.encode (
86
+ {
87
+ 'title' : newProduct.title,
88
+ 'description' : newProduct.description,
89
+ 'price' : newProduct.price,
90
+ 'imageUrl' : newProduct.imageUrl,
91
+ },
92
+ ),
93
+ );
94
+ }
78
95
if (prodIndex >= 0 ) {
79
96
_items[prodIndex] = newProduct;
80
97
notifyListeners ();
Original file line number Diff line number Diff line change @@ -55,32 +55,28 @@ class _EditProductScreenState extends State<EditProductScreen> {
55
55
_isLoading = true ;
56
56
});
57
57
if (_editedProduct.id != null ) {
58
- Provider .of <Products >(context, listen: false )
59
- .updateProduct (_editedProduct.id, _editedProduct);
60
- setState (() {
61
- _isLoading = false ;
62
- });
63
- Navigator .of (context).pop ();
64
- }
65
- try {
66
58
await Provider .of <Products >(context, listen: false )
67
- .addProduct (_editedProduct);
68
- } catch (error) {
69
- return showDialog <Null >(
70
- context: context,
71
- builder: (ctx) => AlertDialog (
72
- title: Text ('An Error occurred' ),
73
- content: Text ('Something went ' ),
74
- actions: < Widget > [
75
- TextButton (
76
- onPressed: () {
77
- Navigator .of (ctx).pop ();
78
- },
79
- child: Text ('Okay' ))
80
- ],
81
- ),
82
- );
83
- } finally {
59
+ .updateProduct (_editedProduct.id, _editedProduct);
60
+ } else {
61
+ try {
62
+ await Provider .of <Products >(context, listen: false )
63
+ .addProduct (_editedProduct);
64
+ } catch (error) {
65
+ return showDialog <Null >(
66
+ context: context,
67
+ builder: (ctx) => AlertDialog (
68
+ title: Text ('An Error occurred' ),
69
+ content: Text ('Something went ' ),
70
+ actions: < Widget > [
71
+ TextButton (
72
+ onPressed: () {
73
+ Navigator .of (ctx).pop ();
74
+ },
75
+ child: Text ('Okay' ))
76
+ ],
77
+ ),
78
+ );
79
+ }
84
80
setState (() {
85
81
_isLoading = false ;
86
82
});
You can’t perform that action at this time.
0 commit comments