【问题标题】:PageController.page cannot be accessed before a PageView is built with it在用它构建 PageView 之前无法访问 PageController.page
【发布时间】:2021-03-25 20:21:15
【问题描述】:

我是 Flutter 和 Getx 的新手,我创建了一个底部导航栏,但导航到页面时出现错误。

错误:断言失败:第 180 行 pos 7:'positions.isNotEmpty':PageController.page 在使用它构建 PageView 之前无法访问

这是我的代码:

class BottomNavBar extends StatelessWidget {
final HomeController homeController = Get.put(new HomeController());
Widget callPage(int current) {
  switch (current) {
    case 0:
      return new Home();
      break;
    case 1:
      return new market();
      break;
    case 2:
      return new setting();
      break;
    case 3:
      return new news();
      break;
    case 4:
      return new Wallet();
      break;
    default:
      return new Home();
  }
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Obx(
      () => Center(
        child: callPage(homeController.selectedNavIndex),
      ),
    ),
    bottomNavigationBar: Obx(
      () => BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: "Home",
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.leaderboard),
              label: "Markets",
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.insights),
              label: "Trades",
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.library_books),
              label: "News",
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.account_balance_wallet),
              label: "Wallets",
            ),
          ],
          currentIndex: homeController.selectedNavIndex,
          onTap: (index) {
            homeController.selectedNavIndex = index;
          }
          ),
        ),
     );
  }
}

【问题讨论】:

    标签: android ios flutter flutter-getx


    【解决方案1】:

    使用前需要判断:

    pageController?.positions?.isNotEmpty == true
                                ? _pageController?.page
                                : 0,
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2014-09-08
      • 2020-12-29
      • 2021-09-10
      相关资源
      最近更新 更多