gpt4 book ai didi

flutter - Flutter-StreamBuilder-Api-页面刷新

转载 作者:行者123 更新时间:2023-12-03 04:56:47 24 4
gpt4 key购买 nike

我的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(),但是如何?

最佳答案

你可以这样写

    Navigator.pushReplacement(context,new MaterialPageRoute(
builder: (context) => new UserDart())).then((onValue) {
fetchPost(); // this will called when it navigates back from UserDart
});

关于flutter - Flutter-StreamBuilder-Api-页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60447720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com