【问题标题】:State Management doesn't work after hot reload热重载后状态管理不起作用
【发布时间】:2021-06-06 01:19:52
【问题描述】:

我正在使用 Obx 来管理我的状态以构建底部导航栏。

return SafeArea(
  
  child: Scaffold(

      bottomNavigationBar: BottomNavBar(),
      body: Obx (() =>
         _getNewSubPage()
      )),
);

我在索引更改时调用 _getNewSubPage()。

 Widget _getNewSubPage() {
switch (homeController.currentIndex.value) {
  case 3:
    return new Container(color: Colors.blue);
  case 2:
    return new Container(
      color: Colors.green[100],
    );
  case 1:
    return new WishlistView();

  default:
    return new CategoryItemListView();
}

}

代码在初始运行和热重启时似乎一切正常,但是当我热重载 _getNewSubPage() 上的视图时不会改变。我也试过 GetBuilder 但发现同样的情况。

控制器代码

  final currentIndex = 0.obs;
  setBottomBarIndex(index) {
   print(index);
   currentIndex.value = index;
 }

【问题讨论】:

    标签: flutter flutter-getx


    【解决方案1】:

    如果您想在其状态发生变化时重建一个小部件,您需要调用该小部件的 setState()。该变量引用了 State 类,当您调用 setState() 时,Flutter 将通过调用 State 类的 build() 方法重建小部件本身。

    【讨论】:

    • 我正在使用 Getx 架构并使用 Obx 来管理状态,并且我的所有视图都在 statelessWidget 内,因此 setState 对我来说不是可行的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 2017-03-13
    • 2020-05-26
    • 2017-05-18
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多