Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
library-app-flutter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ilham Maulana
library-app-flutter
Commits
f3277584
Commit
f3277584
authored
Jul 31, 2024
by
Ilham Maulana
💻
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: book category (handle null)
parent
4c625184
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
110 additions
and
17 deletions
+110
-17
book.dart
lib/src/models/book.dart
+17
-1
category.dart
lib/src/models/category.dart
+6
-0
book_provider.dart
lib/src/providers/book_provider.dart
+43
-0
book_detail.dart
lib/src/widgets/books/book_detail.dart
+15
-7
book_item.dart
lib/src/widgets/books/book_item.dart
+9
-4
book_list.dart
lib/src/widgets/books/book_list.dart
+20
-5
No files found.
lib/src/models/book.dart
View file @
f3277584
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
,
);
);
}
}
}
}
...
...
lib/src/models/category.dart
View file @
f3277584
...
@@ -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
=
[
...
...
lib/src/providers/book_provider.dart
View file @
f3277584
...
@@ -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
"
);
}
}
}
}
lib/src/widgets/books/book_detail.dart
View file @
f3277584
...
@@ -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
),
...
...
lib/src/widgets/books/book_item.dart
View file @
f3277584
...
@@ -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
,
),
),
),
)
],
],
),
),
),
),
...
...
lib/src/widgets/books/book_list.dart
View file @
f3277584
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:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment