我的Widget内部有一个StreamBuilder:
StreamBuilder(
stream: stream.asStream(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
...
该流在void initState内部定义。 fetchPost()从Api调用信息:
@override
void initState() {
super.initState();
setState(() {
stream = fetchPost();
});
}
fetchPost():
Future<List<User>> fetchPost() async {
final response = await http.get('url');
final jsonResponse = json.decode(response.body);
List<User> users = [];
for(var u in jsonResponse){
User user = User(
id: u["id"].toString(),
uname: u["username"],
firstname: u["firstname"],
lastname: u["lastname"],
email: u["email"],
);
users.add(user);
}
return users;
}
在我通过新页面更改列表中的某些内容(API也进行了更改)之后,它导航回到StreamBuilder:
Navigator.pushReplacement(
context,
new MaterialPageRoute(builder: (context) => new UserDart())
);
但是StreamBuilder有时不刷新。我需要做的是刷新数据。因此,每次页面加载时都需要调用fetchPost(),但是如何?
答案 0 :(得分:1)
你可以这样写
Navigator.pushReplacement(context,new MaterialPageRoute(
builder: (context) => new UserDart())).then((onValue) {
fetchPost(); // this will called when it navigates back from UserDart
});