Skip to content

Commit

Permalink
tutorial 9 bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinIW committed Nov 22, 2023
1 parent 07faaf1 commit ef81b86
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
61 changes: 61 additions & 0 deletions lib/models/items.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// To parse this JSON data, do
//
// final item = itemFromJson(jsonString);

import 'dart:convert';

List<Item> itemFromJson(String str) => List<Item>.from(json.decode(str).map((x) => Item.fromJson(x)));

String itemToJson(List<Item> data) => json.encode(List<dynamic>.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<String, dynamic> json) => Item(
model: json["model"],
pk: json["pk"],
fields: Fields.fromJson(json["fields"]),
);

Map<String, dynamic> 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<String, dynamic> json) => Fields(
user: json["user"],
name: json["name"],
amount: json["amount"],
description: json["description"],
);

Map<String, dynamic> toJson() => {
"user": user,
"name": name,
"amount": amount,
"description": description,
};
}
11 changes: 6 additions & 5 deletions lib/screens/list_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -12,10 +13,10 @@ class ProductPage extends StatefulWidget {
}

class _ProductPageState extends State<ProductPage> {
Future<List<Product>> fetchProduct() async {
Future<List<Item>> 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"},
Expand All @@ -25,10 +26,10 @@ Future<List<Product>> fetchProduct() async {
var data = jsonDecode(utf8.decode(response.bodyBytes));

// melakukan konversi data json menjadi object Product
List<Product> list_product = [];
List<Item> 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;
Expand Down Expand Up @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class _LoginPageState extends State<LoginPage> {
// 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,
});
Expand Down
4 changes: 2 additions & 2 deletions lib/screens/shoplist_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ class _ShopFormPageState extends State<ShopFormPage> {
// 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(<String, String>{
'name': _name,
'price': _price.toString(),
'amount': _price.toString(),
'description': _description,

}));
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/shop_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down

0 comments on commit ef81b86

Please sign in to comment.