Skip to content

Instantly share code, notes, and snippets.

@mfebrianto
Last active March 7, 2023 23:04
Show Gist options
  • Save mfebrianto/64dd4aaed4422dc1232905fda807d9d8 to your computer and use it in GitHub Desktop.
Save mfebrianto/64dd4aaed4422dc1232905fda807d9d8 to your computer and use it in GitHub Desktop.
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