Below I have a simple setup to demonstrate a Stateful Widget using a FutureBuilder
. When the Future
async
call first fires we see the CircularProgressIndicator
on the screen.
After the Future is complete we see the UI which includes a refresh FlatButton
, when pressed setState
is called to redraw the widgets.
The Future async function is called again but this time we do not see the CircularProgressIndicator
because AsyncSnapshot.hasData
is true.
This means that AsyncSnapshot.hasData
has its value from the previous FutureBuilder build that completed.
How can I achieve have a "pure" refresh/purge of my UI Widgets so that my CircularProgressIndicator
displays on each each async data call with each Refresh button press that calls setState
?
import 'dart:async';
import 'package:flutter/material.dart';
class DemoFutureBuilder extends StatefulWidget {
@override
State<StatefulWidget> createState() => _DemoFutureBuilder();
}
class _DemoFutureBuilder extends State<DemoFutureBuilder> {
var _displayText = 'Hello World';
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: helloWorldAsync(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
print('=============== Snapshot no data ==========');
return CircularProgressIndicator();
}
print('=============== Snapshot has data ==========');
return Column(
children: <Widget>[
Text(snapshot.data),
Padding(
padding: EdgeInsets.only(top: 20),
child: FlatButton(
child: Text('Pure & Clean Refresh'),
onPressed: () {
setState(() {
_displayText = 'Goodbye Cruel World';
});
},
),
)
],
);
});
}
Future<String> helloWorldAsync() async {
return Future<String>.delayed(Duration(seconds: 3)).then((_) {
return _displayText;
});
}
}
Today, we’ll look at how async methods satisfy a common requirement of background operations: reporting progress. When asynchronous methods report progress, they use an abstraction of the “progress reporter” concept: IProgress<in T>. This interface has a single method: void Report (T value). You can’t get much simpler than that!
Ideally, the progress indicator is shown on top of everything, preventing the user from interacting with the UI. Let’s build a progress indicator that looks like this: What does Flutter provide?
This is the first of Flutter’s inbuilt progress indicators, which is a subclass of the ProgressIndicator abstract class. It is used to communicate the progress of a task in a horizontal bar. This is the second of Flutter’s inbuilt progress indicators, and it is also a subclass of the ProgressIndicator abstract class.
Async/await will gently nudge you away from OOP and towards functional programming. This is natural and should be embraced. Now let’s look at the “receiving” side of progress reports. The caller of the asynchronous method passes in the progress reporter, so it has complete control of how progress reports are handled.
I was also having the same issue i have solved it using "snapshot.connectionState" you can check it as "ConnectionState.(waiting, done or active)" in your case where you are using snapshot.hasData add one more condition for checking connection state. this will solve your problem...
you can refer this for detials https://codinginfinite.com/flutter-future-builder-pagination/
If you add a connection state check in your code like this:
if (!snapshot.hasData || snapshots.connectionState == ConnectionState.waiting) {
print('=============== Snapshot no data ==========');
return CircularProgressIndicator();
}
It should also show the progress indicator when your data gets refreshed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With