From 42a60805a9eca7eece55be6a9b5427e2885215d9 Mon Sep 17 00:00:00 2001 From: Junewoo Date: Thu, 18 Mar 2021 00:12:43 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8B=A4=EC=A0=9C=20DB=EC=97=90=EC=84=9C=20upd?= =?UTF-8?q?ate=20=EB=90=98=EB=8F=84=EB=A1=9D=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20https://github.com/korca0220/Flutter=5Fpro?= =?UTF-8?q?ject=5Fsecond/issues/14?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shop_app/lib/providers/products.dart | 19 +++++++- shop_app/lib/screens/edit_product_screen.dart | 46 +++++++++---------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/shop_app/lib/providers/products.dart b/shop_app/lib/providers/products.dart index 4fd0475..fa162c5 100644 --- a/shop_app/lib/providers/products.dart +++ b/shop_app/lib/providers/products.dart @@ -1,3 +1,5 @@ +import 'dart:html'; + import 'package:flutter/foundation.dart'; import 'dart:convert'; import 'package:http/http.dart' as http; @@ -73,8 +75,23 @@ class Products with ChangeNotifier { } } - void updateProduct(String id, Product newProduct) { + Future updateProduct(String id, Product newProduct) async { final prodIndex = _items.indexWhere((element) => element.id == id); + if (prodIndex > 0) { + final url = + 'https://flutter-shop-app-c87f7-default-rtdb.firebaseio.com/products/$id.json'; + await http.patch( + Uri.parse(url), + body: json.encode( + { + 'title': newProduct.title, + 'description': newProduct.description, + 'price': newProduct.price, + 'imageUrl': newProduct.imageUrl, + }, + ), + ); + } if (prodIndex >= 0) { _items[prodIndex] = newProduct; notifyListeners(); diff --git a/shop_app/lib/screens/edit_product_screen.dart b/shop_app/lib/screens/edit_product_screen.dart index 50479d6..9ed40f3 100644 --- a/shop_app/lib/screens/edit_product_screen.dart +++ b/shop_app/lib/screens/edit_product_screen.dart @@ -55,32 +55,28 @@ class _EditProductScreenState extends State { _isLoading = true; }); if (_editedProduct.id != null) { - Provider.of(context, listen: false) - .updateProduct(_editedProduct.id, _editedProduct); - setState(() { - _isLoading = false; - }); - Navigator.of(context).pop(); - } - try { await Provider.of(context, listen: false) - .addProduct(_editedProduct); - } catch (error) { - return showDialog( - context: context, - builder: (ctx) => AlertDialog( - title: Text('An Error occurred'), - content: Text('Something went '), - actions: [ - TextButton( - onPressed: () { - Navigator.of(ctx).pop(); - }, - child: Text('Okay')) - ], - ), - ); - } finally { + .updateProduct(_editedProduct.id, _editedProduct); + } else { + try { + await Provider.of(context, listen: false) + .addProduct(_editedProduct); + } catch (error) { + return showDialog( + context: context, + builder: (ctx) => AlertDialog( + title: Text('An Error occurred'), + content: Text('Something went '), + actions: [ + TextButton( + onPressed: () { + Navigator.of(ctx).pop(); + }, + child: Text('Okay')) + ], + ), + ); + } setState(() { _isLoading = false; });