I have a Flutter Text
widget and its content is populated from an external REST call.I would like to refresh the widget content periodically every 5 mins by calling
Replace this :
new Timer.periodic(oneSecond, (Timer t) => buildCountWidget());
By this:
new Timer.periodic(oneSecond, (Timer t) => setState((){}));
And it should work, every time you call setState it'll refresh the widget and will call to the Future method again.
UPDATE
It's working fine, if you make these changes, you will notice how the data is refreshed (just for testing):
Future fetchPatientCount() async {
print("fetchPatientCount");
return DateTime.now().toIso8601String();
}
...
new FutureBuilder(
future: fetchPatientCount(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
/* below text needs to be updated every 5 mins or so */
return new Text('#' + snapshot.data.toString(),
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.w700,
fontSize:7.0));
} else if (snapshot.hasError) {
return new Text("${snapshot.error}");
}
}
If the data changes every 25 seconds, it's working , you have to check your fetchPatientCount
method. ( encode the data to json before send requestBody
)