Skip to content

Commit

Permalink
Updated Future Builder widget
Browse files Browse the repository at this point in the history
  • Loading branch information
annshsingh committed Apr 14, 2019
1 parent 617d00d commit 83649d8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/screens/future_builder.dart
Expand Up @@ -33,29 +33,36 @@ class _FutureBuilderWidgetState extends State<FutureBuilderWidget> {
),
body: Center(
child: FutureBuilder<Demo>(
///if future is null then API will not be called as soon as the screen
///If future is null then API will not be called as soon as the screen
///loads. This can be used to make this Future Builder dependent
///on a button click.
future: _isButtonClicked ? getDemoResponse() : null,
builder: (context, snapshot) {
switch (snapshot.connectionState) {

///when the future is null
///when the future is null
case ConnectionState.none:
return Text(
'Press the button to fetch data',
textAlign: TextAlign.center,
);

case ConnectionState.active:

///when data is being fetched
case ConnectionState.waiting:
return CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue));

case ConnectionState.done:
///task is complete with an error (eg. When you
///are offline)
if (snapshot.hasError)
return Text(
'Error:\n\n${snapshot.error}',
textAlign: TextAlign.center,
);
///task is complete with some data
return Text(
'Fetched Data:\n\n${snapshot.data.title}',
textAlign: TextAlign.center,
Expand Down

0 comments on commit 83649d8

Please sign in to comment.