Flutter-GetX-Navigation:加载初始屏幕时出现空检查操作员错误

发布于 2025-01-18 09:38:14 字数 2502 浏览 0 评论 0原文

我是DART的新手,一般而言。在YouTube上观看教程后,我制作了此代码。在大多数情况下,我能够自己解决大多数问题,在这里我觉得我需要一些帮助。我想添加一个底部绘制栏。将其设置为初始根后,我会收到以下错误: -

Null check operator used on a null value

在这里是: -

“在此处输入图像说明”

栏的页面代码:

class LandingPage extends StatelessWidget {
      LandingPage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return GetBuilder<LandingPageController>(
            builder: (controller) {
              return Scaffold(
                body: SafeArea(
                  child:IndexedStack(
                    index: controller.tabIndex,
                    children: const [
                      HomeScreen(),
                      CourseScreen(),
                      ProfileScreen(),
                    ],
    
                  ),
                ),
                bottomNavigationBar: BottomNavigationBar(
    
                  // onTap: Controller.changeTabIndex,
                  // currentIndex: controller.tabIndex,
                  items: const [
                    BottomNavigationBarItem(
                      icon: Icon(Icons.home),
                      label: 'Home',
                      backgroundColor: Color(0xff2AA8A1),
                    ),
                    BottomNavigationBarItem(
                      icon: Icon(Icons.play_arrow),
                      label: 'Course',
                      backgroundColor: Color(0xff2AA8A1),
                    ),
                    BottomNavigationBarItem(
                      icon: Icon(Icons.person_sharp),
                      label: 'Profile',
                      backgroundColor: Color(0xff2AA8A1),
                    ),
                  ],
    
                ), //BottomNavBar().BottomNavigationBar(),
    
              );
            }
        );
      }
    }

带有底部导航

        import 'package:get/get.dart';
    
    class LandingPageController extends GetxController {
      var tabIndex = 0;
    
      void changeTabIndex(int index) {
        tabIndex = index;
        update();
      }
    }

=“ https://i.sstatic.net/toxfi.png 屏幕: -

 _setInitialScreen(User? user) {
if (user == null) {
  Get.offAll(() => LoginScreen());
} else {
  Get.offAll(() => LandingPage());
}

}

请帮助我理解为什么我会遇到此错误

I am very new to Dart, and coding in general. I have produced this code after watching tutorials on YouTube. For the most part, I have been able to troubleshoot most of my problems on my own, here I feel I need some help. I wanted to add a BottomNavigationBar. After I set it as the initial root I am getting the following error:-

Null check operator used on a null value

Here it is:-

enter image description here

The code of page with Bottom nav bar is here:-

class LandingPage extends StatelessWidget {
      LandingPage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return GetBuilder<LandingPageController>(
            builder: (controller) {
              return Scaffold(
                body: SafeArea(
                  child:IndexedStack(
                    index: controller.tabIndex,
                    children: const [
                      HomeScreen(),
                      CourseScreen(),
                      ProfileScreen(),
                    ],
    
                  ),
                ),
                bottomNavigationBar: BottomNavigationBar(
    
                  // onTap: Controller.changeTabIndex,
                  // currentIndex: controller.tabIndex,
                  items: const [
                    BottomNavigationBarItem(
                      icon: Icon(Icons.home),
                      label: 'Home',
                      backgroundColor: Color(0xff2AA8A1),
                    ),
                    BottomNavigationBarItem(
                      icon: Icon(Icons.play_arrow),
                      label: 'Course',
                      backgroundColor: Color(0xff2AA8A1),
                    ),
                    BottomNavigationBarItem(
                      icon: Icon(Icons.person_sharp),
                      label: 'Profile',
                      backgroundColor: Color(0xff2AA8A1),
                    ),
                  ],
    
                ), //BottomNavBar().BottomNavigationBar(),
    
              );
            }
        );
      }
    }

My LandingPage Controller:-

        import 'package:get/get.dart';
    
    class LandingPageController extends GetxController {
      var tabIndex = 0;
    
      void changeTabIndex(int index) {
        tabIndex = index;
        update();
      }
    }

here is my initial Screen:-

 _setInitialScreen(User? user) {
if (user == null) {
  Get.offAll(() => LoginScreen());
} else {
  Get.offAll(() => LandingPage());
}

}

Please Help me understand why I am getting this error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2025-01-25 09:38:14

检查controller.tabindex值的值。可能这就是你变得空的地方。

Check the value of controller.tabindex value. May be that's where you are getting null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文