I want to refresh my List when hitting the back button on the details page (which the List page links from). However, the method I need to run is a Future and it seems that causes some issues.
I have used this approach: https://stackoverflow.com/questions/49933272/how-to-refresh-a-page-after-back-bottun-pressed?rq=1#=
And here is the method:
// Load list items
Future _loadItems() async {
// setState(() => _isLoading = true);
List<CNotification> _listItems = new List<CNotification>();
SharedPreferences prefs = await SharedPreferences.getInstance();
String _usr = prefs.getString('usr');
String _pwd = prefs.getString('pwd');
String _communityId = prefs.getInt('communityid').toString();
final response = await http.get(helperConnectionString +
'GetNotifications?usr=$_usr&pwd=$_pwd&communityid=$_communityId');
if (response.statusCode == 200) {
// If the call to the server was successful, parse the JSON
List data = json.decode(response.body);
for (var i = 0; i < data.length; i++) {
String _title = "";
data[i]['MessageText'].toString().length > 25
? _title =
data[i]['MessageText'].toString().substring(0, 25) + '...'
: _title = data[i]['MessageText'].toString();
DateTime entryCreated =
DateTime.parse(data[i]['DateCreated'].toString());
String _dRead =
DateFormat.yMd().add_jms().format(entryCreated).toString();
_listItems.add(new CNotification(int.parse(data[i]['Id'].toString()),
_title, _dRead, data[i]['DateRead'] == null ? false : true));
}
setState(() {
_items = _listItems;
_isLoading = false;
});
} else {
setState(() {
_isLoading = false;
});
print('Error');
}
}
Anyone?
/Bob