当我点击详细信息页面上的后退按钮时(列表页面链接到该页面),我想刷新我的列表。但是,我需要运行的方法是Future,并且似乎会引起一些问题。
我使用了这种方法:https://stackoverflow.com/questions/49933272/how-to-refresh-a-page-after-back-bottun-pressed?rq=1#=
这是方法:
// 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');
}
}
有人吗?
/鲍勃
答案 0 :(得分:0)
这是您要找的东西。
Navigator.of(context).push(MaterialPageRoute(builder: (context) => SomePage())).then((_) => _loadItems())
但是我建议您在任何地方使用async
/ await
:
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: _showSomePage,
child: Text('Show some page'),
);
}
void _showSomePage() async {
await Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SomePage(),
));
await _loadItems();
}
Future<void> _loadItems() async {
// ...
}
内联版本:
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () async {
await Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SomePage(),
));
await _loadItems();
},
child: Text('Show some page'),
);
}