我正在编写带有波动的应用程序,在页面A中将wantKeepAlive设置为true,然后在initState生命周期中请求数据。现在,我跳到页面B并执行某些操作,然后返回页面A,我想再次请求数据,因为我在页面B中执行了某些操作,但是页面A保持活动状态,并且从未运行initState。所以,我应该如何再次请求数据?
答案 0 :(得分:0)
我相信这就是您想要的:
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'),
)
],
)
);
}
}
答案 1 :(得分:0)
我有自己的解决方案
我有四个页面,并用另一种状态的综合浏览量小工具包装。
当其他页面值发生变化时,我想用api调用future。
在我的情况下,我的代码之一都不调用initState。
因此,我使用didUpdateWidget并让wantKeepAlive查找状态小部件。
我也使用提供程序来更改值和全局注册。
我认为您可以将redux用于全局寄存器值。
下面是我的代码。希望对您有帮助。
Pageview.dart
Widget _buildPageView($user) {
return PageView(
physics: NeverScrollableScrollPhysics(),
controller: _pageController,
onPageChanged: (index) {
pageChanged(index);
},
children: <Widget>[
HomePage(pageCtrl:_pageController, keepHeight: $user.userKeepHeight), /// this is the page that I call didUpdateWidget when another value is change. And keepHeight is look up provider value. So, it can be changing dynamically.
SearchPage(pageCtrl:_pageController),
ShopPage(),
MyPage(),
],
);
}
HomePage.dart
class HomePage extends StatefulWidget {
HomePage({Key key, this.pageCtrl, this.keepHeight}) : super(key: key);
final PageController pageCtrl;
bool keepHeight;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
bool _once = true;
Future _homeFuture;
final dio = Dio();
int _totCnt = 0;
int _listCnt = 0;
final ScrollController _homeScrollCtrl = new ScrollController();
bool keepAlive = true;
var baseUrl = 'https://rest.pickling.kr';
List<HomeDTO> list1;
getHomeContentsList(i,j) async {
Response response;
try {
response = await dio.get("$baseUrl/admin/131/home",
queryParameters: {
'offset':0,
'limit': 30
},
options: Options(headers: {"Authorization": i, "User-Agent": j}));
if (response.statusCode == 200) {
print(response.data['result']);
_totCnt = int.parse(response.data['result']['totCnt']);
_listCnt = int.parse(response.data['result']['listCnt']);
var les1 = response.data['result']['homeList'];
list1 = await les1.map<HomeDTO>((json) => HomeDTO.fromJson(json)).toList();
}
} catch (e) {
print(e);
}
}
@override
void didUpdateWidget(oldWidget) {
super.didUpdateWidget(oldWidget);
UserState $user = Provider.of<UserState>(context, listen: false);
QurateState $qurate = Provider.of<QurateState>(context, listen: false);
if (widget.keepHeight != oldWidget.keepHeight) {
setState(() {
keepAlive = widget.keepHeight;
});
if(widget.keepHeight == false) {
setState(() {
keepAlive = false;
});
updateKeepAlive();
$user.setUserKeepHeight(true);
_homeFuture = _getHomeFuture($user, $qurate);
} else {
setState(() {
keepAlive = true;
});
updateKeepAlive();
}
}
}
@override
void initState() {
super.initState();
UserState $user = Provider.of<UserState>(context, listen: false);
QurateState $qurate = Provider.of<QurateState>(context, listen: false);
_homeFuture = _getHomeFuture($user, $qurate);
}
_getHomeFuture($user, $qurate) async{
await getHomeContentsList($user.accessToken, $user.appInfo);
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => keepAlive;
@override
Widget build(BuildContext context) {
super.build(context);
return widget code}
总结一下,当另一个值更改时,提供者注意到更改了值。 更改后的值正在发生变化。
然后,当用户首次进入首页时,请调用initState。 然后用户又一次进入首页initState和didUpdateWidget,他们都没有被调用。
但是,另一个值发生了变化,首页名为didUpdateWidget,名称为future。
因此它可以更新主页。
对不起,代码太长:X