gpt4 book ai didi

flutter - flutter keep alive widget 如何再次请求数据?

转载 作者:行者123 更新时间:2023-12-05 07:15:56 37 4
gpt4 key购买 nike

我在用flutter写一个app,在A页面设置wantKeepAlive为true,然后在initState生命周期请求数据。现在,我跳转到B页面做了一些事情,然后又回到了A页面,我想再次请求数据,因为我在B页面做了一些事情,但是A页面一直保持事件状态,它从未运行过initState。那么,我应该如何重新请求数据呢?

最佳答案

我相信这就是您要找的:

class PageA extends StatefulWidget {
@override
_PageAState createState() => _PageAState();
}

class _PageAState extends State<PageA> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page A'),
),
body: Column(
children: <Widget>[
Text('Page A'),
RaisedButton(
onPressed: goToPageB,
child: Text('Go to Page B'),
)
],
)
);
}

void goToPageB(){
Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return PageB();
}
)).then((result){
// Once you come back from Page B you can
// fetch your data for page A again here
});
}
}

class PageB extends StatefulWidget {
@override
_PageBState createState() => _PageBState();
}

class _PageBState extends State<PageB> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Page B'),
),
body: Column(
children: <Widget>[
Text('Page B'),
RaisedButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('Go to Page A'),
)
],
)
);
}
}

关于flutter - flutter keep alive widget 如何再次请求数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422143/

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