使用自动keepparealiveclientmixin时dispose()是调用的
我的印象是,使用自动keepparealiveclientmixin
将阻止状态dispose()
在窗口窗口不再可见时被调用。
但是,我有一个情况下,即使我实现了automatickeakealiveclientmixin distose()
and and initstate()也会被调用。 >正确。
class IdleScreenState extends State<IdleScreen> with AutomaticKeepAliveClientMixin {
@override
void initState() {
super.initState();
print('IdleScreen initState');
}
@override
void dispose() {
print('IdleScreen dispose');
super.dispose();
}
@override
Widget build(BuildContext context) {
super.build(context);
// ...build the page...
}
@override
bool get wantKeepAlive => true;
}
这就是我
class MainScreen extends State<MainScreen> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return somecondition ? IdleScreen() : OtherScreen();
}
@override
bool get wantKeepAlive => true;
}
每次显示此窗口小部件(屏幕)时隐藏/显示此窗口小部件的方式,initstate()
被调用,每次我隐藏它时,dispose()
get get称为。好像自动keepparealiveclientmixin
没有效果。我发现的所有其他类似问题似乎是由于缺少Want keepareive =&gt; true
或super.build(context)
,但它们在代码中为100%。
我也尝试为为
indlescreen
提供globalkey
,但这没有任何效果。
但是,如果我使用indexedStack
或off stage
hide/显示窗口小部件,则可以按预期工作(initstate() ()
在隐藏/显示小部件时不会被调用)。
IndexedStack(
index: somecondition ? 0 : 1,
children: [
IdleScreen(),
OtherScreen()
],
),
也许我误会了,但是自动keepakealiveclientmixin
不必手动使用此技术将小部件保持在围绕窗口上的全部目的吗?
如果这很重要,这是在网络项目中。
I am under the impression that using AutomaticKeepAliveClientMixin
would prevent the states dispose()
callback from being called when the Widget isn't visible anymore.
However, I have a situation where dispose()
and initState()
get called every time I hide/show a Widget, even though I implemented AutomaticKeepAliveClientMixin
correctly.
class IdleScreenState extends State<IdleScreen> with AutomaticKeepAliveClientMixin {
@override
void initState() {
super.initState();
print('IdleScreen initState');
}
@override
void dispose() {
print('IdleScreen dispose');
super.dispose();
}
@override
Widget build(BuildContext context) {
super.build(context);
// ...build the page...
}
@override
bool get wantKeepAlive => true;
}
This is how I hide/show this Widget
class MainScreen extends State<MainScreen> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return somecondition ? IdleScreen() : OtherScreen();
}
@override
bool get wantKeepAlive => true;
}
Every time this Widget (screen) is shown, initState()
gets called, and every time I hide it, dispose()
gets called. It's as if the AutomaticKeepAliveClientMixin
has no effect. All other similar issues I could find seem to be due to either missing the wantKeepAlive => true
or the super.build(context)
, but they are 100% there in the code.
I tried supplying a GlobalKey
for IdleScreen
as well, but that didn't have any effect.
However, if I use an IndexedStack
or Offstage
to hide/show the widget, it works as expected (initState()
and dispose()
don't get called when hiding/showing the widget).
IndexedStack(
index: somecondition ? 0 : 1,
children: [
IdleScreen(),
OtherScreen()
],
),
Maybe I'm mistaken, but isn't the whole purpose of AutomaticKeepAliveClientMixin
to not have to manually keep the widget around using this technique?
This is in a web project, if that matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类型参数t是混合此类的状态的状态下类的类型
。
The type argument T is the type of the StatefulWidget subclass of the State into which this class is being mixed.
you have to pass the widget class name like this..