Commit f3277584 authored by Ilham Maulana's avatar Ilham Maulana 💻

fix: book category (handle null)

parent 4c625184
import "package:library_app/src/models/category.dart";
class Book { class Book {
int id; int id;
String title; String title;
...@@ -10,13 +12,27 @@ class Book { ...@@ -10,13 +12,27 @@ class Book {
this.category); this.category);
factory Book.fromJson(Map<String, dynamic> data) { factory Book.fromJson(Map<String, dynamic> data) {
if (data["category_detail"] != null) {
final Category category = Category.fromJson(
data["category_detail"],
);
return Book(
data["id"],
data["title"],
data["author"],
data["description"],
data["cover_image"],
category.name,
);
}
return Book( return Book(
data['id'] as int, data['id'] as int,
data['title'] as String, data['title'] as String,
data['author'] as String, data['author'] as String,
data['description'] as String, data['description'] as String,
data['coverUrl'] as String?, data['coverUrl'] as String?,
data['category'] as String?, null,
); );
} }
} }
......
...@@ -2,6 +2,12 @@ class Category { ...@@ -2,6 +2,12 @@ class Category {
String name; String name;
Category(this.name); Category(this.name);
factory Category.fromJson(Map<String, dynamic> data) {
return Category(
data['name'] as String,
);
}
} }
final initialCategories = [ final initialCategories = [
......
...@@ -50,4 +50,47 @@ class BookProvider with ChangeNotifier { ...@@ -50,4 +50,47 @@ class BookProvider with ChangeNotifier {
debugPrint("Error: Fetch books failed, $error"); debugPrint("Error: Fetch books failed, $error");
} }
} }
Future<void> getCategories() async {
try {
final response = await http.get(
Uri.parse('$baseUrl/categories'),
headers: {'Content-Type': 'application/json'},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
books = data["results"];
} else {
final code = response.statusCode;
debugPrint("Error: Fetch books failed, $code");
}
notifyListeners();
} catch (error) {
debugPrint("Error: Fetch books failed, $error");
}
}
Future<void> getCategoryById(int id) async {
try {
final response = await http.get(
Uri.parse('$baseUrl/categories/$id'),
headers: {'Content-Type': 'application/json'},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
var category = data["results"];
return category;
} else {
final code = response.statusCode;
debugPrint("Error: Fetch books failed, $code");
}
notifyListeners();
} catch (error) {
debugPrint("Error: Fetch books failed, $error");
}
}
} }
...@@ -40,14 +40,22 @@ class _BookDetail extends State<BookDetail> { ...@@ -40,14 +40,22 @@ class _BookDetail extends State<BookDetail> {
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
), ),
Text( Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(
'By ${book.author}', 'By ${book.author}',
style: Theme.of(context).textTheme.labelMedium, style: Theme.of(context).textTheme.labelMedium,
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
Text( ),
Offstage(
offstage: book.category == null,
child: Badge(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
label: Text(
book.category ?? "", book.category ?? "",
style: Theme.of(context).textTheme.labelSmall, ),
),
), ),
Padding( Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0), padding: const EdgeInsets.symmetric(vertical: 10.0),
......
...@@ -63,10 +63,15 @@ class BookItem extends StatelessWidget { ...@@ -63,10 +63,15 @@ class BookItem extends StatelessWidget {
), ),
], ],
), ),
Text( Offstage(
offstage: _book.category == null,
child: Badge(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
label: Text(
_book.category ?? "", _book.category ?? "",
style: Theme.of(context).textTheme.labelMedium,
), ),
),
)
], ],
), ),
), ),
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:library_app/src/models/category.dart';
import 'package:library_app/src/providers/book_provider.dart'; import 'package:library_app/src/providers/book_provider.dart';
import 'package:library_app/src/widgets/books/book_item.dart'; import 'package:library_app/src/widgets/books/book_item.dart';
...@@ -25,16 +26,30 @@ class _BookList extends State<BookList> { ...@@ -25,16 +26,30 @@ class _BookList extends State<BookList> {
return Consumer<BookProvider>( return Consumer<BookProvider>(
builder: (context, bookProvider, child) { builder: (context, bookProvider, child) {
if (bookProvider.books != null) { if (bookProvider.books != null) {
final Iterable<Book> books = bookProvider.books!.map( final Iterable<Book> books = bookProvider.books!.map((book) {
(book) => Book( if (book["category_detail"] != null) {
final Category category = Category.fromJson(
book["category_detail"],
);
return Book(
book["id"], book["id"],
book["title"], book["title"],
book["author"], book["author"],
book["description"], book["description"],
book["cover_image"], book["cover_image"],
book["category"], category.name,
),
); );
}
return Book(
book["id"],
book["title"],
book["author"],
book["description"],
book["cover_image"],
null,
);
});
return NestedScrollView( return NestedScrollView(
headerSliverBuilder: headerSliverBuilder:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment