Getx() 并隐藏底部导航栏 - 有状态小部件呈现错误

时间:2021-06-21 18:39:12

标签: flutter getx flutter-getx

我的无状态小部件中有一个有状态小部件 (miniplayer)。当我使用 Obx 时,我试图隐藏底部导航栏并收到以下错误...知道如何解决这个问题吗?

I/flutter (9567):==╡ 小部件库的例外情况╞=========================== ============================ I/flutter (9567):在构建 ValueListenableBuilder(dirty,dependencies) 时抛出了以下断言: I/flutter(9567):[_LocalizationsScope-[GlobalKey#87424],_InheritedTheme],状态: 我/颤振(9567):_ValueListenableBuilderState#23ae3): I/flutter(9567):在构建过程中调用 setState() 或 markNeedsBuild()。 I/flutter (9567):这个 Obx 小部件不能被标记为需要构建,因为框架已经在处理中 I/flutter(9567):构建小部件。只有在构建阶段,小部件才能被标记为需要构建 I/flutter (9567):它的一个祖先目前正在建造。允许此异常,因为框架构建 I/flutter ( 9567):在子级之前的父级小部件,这意味着总是会构建一个脏的后代。否则, I/flutter (9567):框架在这个构建阶段可能不会访问这个小部件。 I/flutter (9567):调用 setState() 或 markNeedsBuild() 的小部件是: I/颤振(9567):Obx I/flutter ( 9567):发出违规调用时当前正在构建的小部件是: I/flutter(9567):ValueListenableBuilder 我/颤振(9567): I/flutter (9567):相关的导致错误的小部件是: I/flutter (9567):迷你播放器

底部导航代码

 bottomNavigationBar: Container(
          child: Obx(
            () => !_videoController.showBottomNavigation.value
                ? SizedBox.shrink()
                : BottomNavigationBar(
                    currentIndex: 0,
                    fixedColor: Colors.blue,
                    items: [
                      BottomNavigationBarItem(
                        icon: Icon(Icons.home),
                        label: 'Home',
                      ),
                      BottomNavigationBarItem(
                        icon: Icon(Icons.mail),
                        label: 'Messages',
                      ),
                      BottomNavigationBarItem(
                        icon: Icon(Icons.person),
                        label: 'Profile',
                      )
                    ],
                  ),
          ),

1 个答案:

答案 0 :(得分:0)

尝试使用 isTrueisFalse 声明 bool 条件。然后包装 UI 更改。

bottomNavigationBar: Container(
          child: Obx(
            (){ return _videoController.showBottomNavigation.isFalse
                ? SizedBox.shrink()
                : BottomNavigationBar(
                    currentIndex: 0,
                    fixedColor: Colors.blue,
                    items: [
                      BottomNavigationBarItem(
                        icon: Icon(Icons.home),
                        label: 'Home',
                      ),
                      BottomNavigationBarItem(
                        icon: Icon(Icons.mail),
                        label: 'Messages',
                      ),
                      BottomNavigationBarItem(
                        icon: Icon(Icons.person),
                        label: 'Profile',
                      )
                    ],
                  ),
          }),