Dart 当父对象为rebuild-FLATTER时,调用子初始化方法
据我所知,一个有状态的小部件方法在小部件树中的第一个构建时只被调用一次,而构建方法在每次状态改变或父级重建时被调用Dart 当父对象为rebuild-FLATTER时,调用子初始化方法,dart,flutter,hybrid-mobile-app,Dart,Flutter,Hybrid Mobile App,据我所知,一个有状态的小部件方法在小部件树中的第一个构建时只被调用一次,而构建方法在每次状态改变或父级重建时被调用 bottomNavigationBar: BottomNavigationBar(items: [ BottomNavigationBarItem(icon: new Icon(Icons.home,), title: new Text("HOME", style: new TextStyle(fontSize: 11.0),),), BottomNavi
bottomNavigationBar: BottomNavigationBar(items: [
BottomNavigationBarItem(icon: new Icon(Icons.home,), title: new Text("HOME", style: new TextStyle(fontSize: 11.0),),),
BottomNavigationBarItem(icon: new Icon(Icons.message,), title: new Text("MESSAGES", style: new TextStyle(fontSize: 11.0),),),
BottomNavigationBarItem(icon: new Icon(Icons.notifications,), title: new Text("NOTIFICATIONS", style: new TextStyle(fontSize: 11.0),),),
BottomNavigationBarItem(icon: new Icon(Icons.assignment,), title: new Text("MENTOR", style: new TextStyle(fontSize: 11.0),),),
BottomNavigationBarItem(icon: new Icon(Icons.account_circle,), title: new Text("PROFILE", style: new TextStyle(fontSize: 11.0),),),
],
onTap: (int index){
setState(() {
_currentActiveIndex = index;
});
},
currentIndex: _currentActiveIndex,
type: BottomNavigationBarType.shifting,
),
body: _getCurrentPage(_currentActiveIndex),
_这里的currentActiveIndex是一个类的状态之一,其中基于_currentActiveIndex的值呈现一个单独的主体小部件
body: TabBarView(children: <Widget>[
new PostLoadWidget.express(),
new PostLoadWidget.confess(),
]),
body:tabbar视图(子项:[
新建PostLoadWidget.express(),
新的PostLoadWidget.Fancy(),
]),
这是scaffold小部件的主体:-基于上面的_currentActiveIndex呈现的小部件
class PostLoadWidgetState extends State<PostLoadWidget> {
@override
void initState(){
print("initState is called here);
super.initState();
}
}
类PostLoadWidgetState扩展状态{
@凌驾
void initState(){
print(此处称为initState);
super.initState();
}
}
每次重建(上面)父级时,如果更改了_curentActiveIndex,就会调用PostLoadWidgetState initState()方法,这是所需的行为。我一直在初始化initState()中的网络调用我不希望每次重建其父级时都调用它。您可以使用mixin
automatickepaliveclientmixin
并覆盖wantKeepAlive
getter并返回true,以避免每次重新创建状态
class PostLoadWidgetState extends State<PostLoadWidget> with AutomaticKeepAliveClientMixin<PostLoadWidget> {
...
@override
bool get wantKeepAlive => true;
}
类PostLoadWidgetState使用AutomaticEpaLiveClientMixin扩展状态{
...
@凌驾
bool get wantKeepAlive=>true;
}
此处的更多信息:我已经尝试使用AutomaticKeepAliveClient mixin,并在wantKeepAlive上返回true。此外,根据文档,这是状态的默认行为,无法解决我的问题。