重建父级时调用子级init方法-flutter [英] child init method is called when parent is rebuild - flutter

查看:239
本文介绍了重建父级时调用子级init方法-flutter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Flutter的工作机制只有在状态树第一次在窗口树中构建时才被调用一次,而每次状态改变或父对象被重建时都会被调用build方法。

As far as i understand and working mechanism of flutter a stateful widget method is only called once when its first build in the widget tree and build method method is called every time when its state is changed or the parent is rebuild.

 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的值呈现单独的正文小部件。

_currentActiveIndex here is one of the state of a class where a seperate body widget is rendered based on the value of _currentActiveIndex.

 body: TabBarView(children: <Widget>[
      new PostLoadWidget.express(),
      new PostLoadWidget.confess(),
    ]),

这是脚手架小部件的主体:-基于渲染的小部件在上面的_currentActiveIndex中。

This is the body of scaffold widget:- a widget rendered based on the _currentActiveIndex above.

class PostLoadWidgetState extends State<PostLoadWidget> {
   @override
   void initState(){
       print("initState is called here);
       super.initState();
    }
}

每次在_curentActiveIndex发生更改的地方重建父项(上方),都会调用PostLoadWidgetState initState()方法,其中是我们期望的行为。我一直在initState()中初始化网络调用,我不想在每次父项重建时都调用它。

Every time parents is rebuild (above) where the _curentActiveIndex is changed the PostLoadWidgetState initState() method is called which is the desired behaviour. I have been initializing networking call in initState() which i don't want to call on every rebuild of its parents.

推荐答案

您可以使用mixin AutomaticKeepAliveClientMixin 并覆盖 wantKeepAlive getter并返回true以避免重新创建State

You can use the mixin AutomaticKeepAliveClientMixin and override the wantKeepAlive getter and return true to avoid recreating the State every time.

        class PostLoadWidgetState extends State<PostLoadWidget> with AutomaticKeepAliveClientMixin<PostLoadWidget> {


            ...

            @override
            bool get wantKeepAlive => true;

        }

此处提供更多信息: https://medium.com/@diegoveloper/flutter -persistent-tab-bars-a26220d322bc

这篇关于重建父级时调用子级init方法-flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆