diff --git a/lib/models/items.dart b/lib/models/items.dart new file mode 100644 index 0000000..e1ce680 --- /dev/null +++ b/lib/models/items.dart @@ -0,0 +1,61 @@ +// To parse this JSON data, do +// +// final item = itemFromJson(jsonString); + +import 'dart:convert'; + +List itemFromJson(String str) => List.from(json.decode(str).map((x) => Item.fromJson(x))); + +String itemToJson(List data) => json.encode(List.from(data.map((x) => x.toJson()))); + +class Item { + String model; + int pk; + Fields fields; + + Item({ + required this.model, + required this.pk, + required this.fields, + }); + + factory Item.fromJson(Map json) => Item( + model: json["model"], + pk: json["pk"], + fields: Fields.fromJson(json["fields"]), + ); + + Map toJson() => { + "model": model, + "pk": pk, + "fields": fields.toJson(), + }; +} + +class Fields { + int user; + String name; + int amount; + String description; + + Fields({ + required this.user, + required this.name, + required this.amount, + required this.description, + }); + + factory Fields.fromJson(Map json) => Fields( + user: json["user"], + name: json["name"], + amount: json["amount"], + description: json["description"], + ); + + Map toJson() => { + "user": user, + "name": name, + "amount": amount, + "description": description, + }; +} diff --git a/lib/screens/list_product.dart b/lib/screens/list_product.dart index 1d7fa2b..0b81010 100644 --- a/lib/screens/list_product.dart +++ b/lib/screens/list_product.dart @@ -3,6 +3,7 @@ import 'package:http/http.dart' as http; import 'dart:convert'; import 'package:shopping_list/models/product.dart'; import 'package:shopping_list/widgets/left_drawer.dart'; +import 'package:shopping_list/models/items.dart'; class ProductPage extends StatefulWidget { const ProductPage({Key? key}) : super(key: key); @@ -12,10 +13,10 @@ class ProductPage extends StatefulWidget { } class _ProductPageState extends State { -Future> fetchProduct() async { +Future> fetchProduct() async { // url ini var url = Uri.parse( - 'http://127.0.0.1:8000/json/'); + 'https://kevin-ignatius-tugas.pbp.cs.ui.ac.id/json/'); var response = await http.get( url, headers: {"Content-Type": "application/json"}, @@ -25,10 +26,10 @@ Future> fetchProduct() async { var data = jsonDecode(utf8.decode(response.bodyBytes)); // melakukan konversi data json menjadi object Product - List list_product = []; + List list_product = []; for (var d in data) { if (d != null) { - list_product.add(Product.fromJson(d)); + list_product.add(Item.fromJson(d)); } } return list_product; @@ -77,7 +78,7 @@ Widget build(BuildContext context) { ), ), const SizedBox(height: 10), - Text("${snapshot.data![index].fields.price}"), + Text("${snapshot.data![index].fields.amount}"), const SizedBox(height: 10), Text( "${snapshot.data![index].fields.description}") diff --git a/lib/screens/login.dart b/lib/screens/login.dart index d38b0d3..8453318 100644 --- a/lib/screens/login.dart +++ b/lib/screens/login.dart @@ -71,7 +71,7 @@ class _LoginPageState extends State { // url ini // Untuk menyambungkan Android emulator dengan Django pada localhost, // gunakan URL http://10.0.2.2/ - final response = await request.login("http://127.0.0.1:8000/auth/login/", { + final response = await request.login("https://kevin-ignatius-tugas.pbp.cs.ui.ac.id/auth/login/", { 'username': username, 'password': password, }); diff --git a/lib/screens/shoplist_form.dart b/lib/screens/shoplist_form.dart index 7f00b64..76fc67e 100644 --- a/lib/screens/shoplist_form.dart +++ b/lib/screens/shoplist_form.dart @@ -125,10 +125,10 @@ class _ShopFormPageState extends State { // Kirim ke Django dan tunggu respons // url final response = await request.postJson( - "http://127.0.0.1:8000//create-flutter/", + "https://kevin-ignatius-tugas.pbp.cs.ui.ac.id/create-flutter/", jsonEncode({ 'name': _name, - 'price': _price.toString(), + 'amount': _price.toString(), 'description': _description, })); diff --git a/lib/widgets/shop_card.dart b/lib/widgets/shop_card.dart index d92db3d..bd4938f 100644 --- a/lib/widgets/shop_card.dart +++ b/lib/widgets/shop_card.dart @@ -57,7 +57,7 @@ class ShopCard extends StatelessWidget { else if (item.name == "Logout") { final response = await request.logout( // url! - "http://127.0.0.1:8000//auth/logout/"); + "https://kevin-ignatius-tugas.pbp.cs.ui.ac.id/auth/logout/"); String message = response["message"]; if (response['status']) { String uname = response["username"];