【问题标题】:Flutter: Getx navigation return nullFlutter:Getx 导航返回 null
【发布时间】:2023-01-20 14:09:34
【问题描述】:

我使用 GetX 包已经有一段时间了,但有时我会遇到一些错误..

现在我有一个 bottomNavigationBar,其中有 5 个页面可以在(优惠 - 类别 - 购物车 - 收藏夹 - 帐户)之间导航。

我的问题是:

当我从索引 0 转到索引 2(例如)时,它正常运行,但是当我想返回索引 0 时,应用程序崩溃并给我这个错误:

用于空值的空检查运算符

与我在另一个项目中使用的方式相同,但是我使用的是TabBar,我正常使用它没有这个错误,但是在底部导航栏中它发生了。

其实我不相信错误是因为小部件种类,但真的想解决它。

笔记 :

我创建了一个 HomePageController,我定义了所有 bottomNavigationBar 操作,比如更改索引和页面列表,..等

对于每个页面,它都有自己的控制器,即使我回到使用HomePageController 的页面,它也会崩溃!!!

这是我的代码的一个简单的:

class HomePageController extends GetxController {
  static HomePageController instance = HomePageController();

  late TextEditingController categoriesSearchController;

  @override
  void onInit() {
    super.onInit();
    categoriesSearchController = TextEditingController();
  }

  int bottomNavIndex = 0;

  changeBottomIndex(int index) {
    bottomNavIndex = index;
    update();
  }


  List<Widget> bottomScreens = const [
    Offers(),
    Categories(),
    Cart(),
    Favorite(),
    Account(),
  ];

  List<ItemModel> meatsList = [
    ItemModel(
      title: 'Thigh',
      image: 'assets/images/home_page/pin_thigh.png',
      description: '1 Kg',
      price: 1.72,
    ),
    ItemModel(
      title: 'Breast',
      image: 'assets/images/home_page/breasts2.jpg',
      description: '1 Kg',
      price: 1.65,
    ),
    ItemModel(
      title: 'lamb',
      image: 'assets/images/home_page/lamb.jpeg',
      description: '1 Kg',
      price: 6.55,
    ),
  ];
}
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomePageController>(
      builder: (controller) => controller != null
          ? SafeArea(
              child: Scaffold(
                backgroundColor: AppColors.whiteColor,
                bottomNavigationBar: BottomNavigationBar(
                  items: controller.changingBottom(),
                  currentIndex: controller.bottomNavIndex,
                  type: BottomNavigationBarType.fixed,
                  selectedItemColor: AppColors.onBoardingButton,
                  onTap: (index) {
                    controller.changeBottomIndex(index);
                  },
                ),
                body: controller.bottomScreens[controller.bottomNavIndex],
              ),
            )
          : const Center(
              child: CircularProgressIndicator(),
            ),
    );
  }
}

【问题讨论】:

  • 朋友们不要让朋友们使用 GetX!如果你在 flutter discord 上,输入 ?getx。否则,这个七分钟的视频对“为什么不getx”进行了很好的详细描述:youtu.be/zlIgy4es5Ts
  • 在里面项目BottomNavigationBar的参数,你定义为controller.changingBottom(),但是controller文件中没有这样的
  • @CavinMacwan 在这里,我想你已经清楚地阅读了代码,thx

标签: flutter navigation flutter-getx


【解决方案1】:

在您的主页中,在 GetBuilder 中添加 init 方法和 autoRemove,如下所示:

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomePageController>(
      init:HomePageController(),
      autoRemove:false,
      builder: (controller) => controller != null
          ? SafeArea(
              child: Scaffold(
                backgroundColor: AppColors.whiteColor,
                bottomNavigationBar: BottomNavigationBar(
                  items: controller.changingBottom(),
                  currentIndex: controller.bottomNavIndex,
                  type: BottomNavigationBarType.fixed,
                  selectedItemColor: AppColors.onBoardingButton,
                  onTap: (index) {
                    controller.changeBottomIndex(index);
                  },
                ),
                body: controller.bottomScreens[controller.bottomNavIndex],
              ),
            )
          : const Center(
              child: CircularProgressIndicator(),
            ),
    );
  }
}

通过使用init,如果它被处置,它将重建你的控制器。并且,通过对 false 使用 autoRemove,它不会每次都处理控制器。

【讨论】:

    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多