【发布时间】: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