Last active
March 7, 2023 23:04
-
-
Save mfebrianto/64dd4aaed4422dc1232905fda807d9d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReadScreen extends StatefulWidget { | |
final String bookId; | |
final LibraryService libraryService; | |
const ReadScreen({ | |
Key? key, | |
required this.bookId, | |
this.libraryService = const LibraryService(), | |
}) : super(key: key); | |
@override | |
State<StatefulWidget> createState() => _ReadScreenState(); | |
} | |
class _ReadScreenState extends State<ReadScreen> { | |
@override | |
void initState() { | |
super.initState(); | |
_bookResult = _fetchBook(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: _buildFutureBuilder(context), | |
); | |
} | |
FutureBuilder _buildFutureBuilder(BuildContext context) { | |
return FutureBuilder( | |
future: _bookResult, | |
builder: (context, snapshot) => _buildContent( | |
context, | |
snapshot.data, | |
), | |
); | |
} | |
Widget _buildContent(BuildContext, data) { | |
return Text(data.value) | |
} | |
Future<String> _fetchBook() async { | |
String bookContent = | |
await widget.libraryService.getBook(bookId); | |
return Result(value: bookContent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment