Flutter 博客应用程序中对 null 值使用的 Null 检查运算符

发布于 2025-01-11 06:58:33 字数 1591 浏览 0 评论 0原文

构建 Expanded(flex: 1) 时抛出以下 _CastError: 对空值使用空检查运算符

相关的导致错误的小部件是: 扩展扩展:file:///home/abdallah/StudioProjects/gei/lib/news_page.dart:45:13

Expanded(child: GetBuilder<HomeController>(builder:(value) {
          if(value.newss.isNotEmpty){
            return ListView.builder(

              itemCount: value.newss.length,
              itemBuilder: (context, index) {
                return  Padding(padding: const EdgeInsets.symmetric(horizontal: 15.0,vertical: 5.0),
                child: Post(model: value.newss[index],),
                );
                
              },
            );
          }else{
            return const Center(child: Text("No News Avilable"),);
          }
        },

        ),
        
        )

HomeController 代码

    class HomeController extends GetxController{
  final FirebaseFunctions _functions = FirebaseFunctions();
  final ScrollController controller =  ScrollController();
  List<NewsModel> newss = [];
  var isLoding = false.obs;
  void getData() async{
    newss.addAll(await _functions.getNews());
    update();
  }
  @override
  void onInit() {
    super.onInit();
    getData();
    controller.addListener(() {
      _functions.isLoading.listen((p) {
        isLoding.value = p;
      });
      double maxScrollPoint = controller.position.maxScrollExtent;
      double currentPosition = controller.position.pixels;
      double height20 = Get.size.height *0.20;
      if(maxScrollPoint - currentPosition <= height20){
        getData();
      }
    });
  }
}

The following _CastError was thrown building Expanded(flex: 1):
Null check operator used on a null value

The relevant error-causing widget was:
Expanded Expanded:file:///home/abdallah/StudioProjects/gei/lib/news_page.dart:45:13

Expanded(child: GetBuilder<HomeController>(builder:(value) {
          if(value.newss.isNotEmpty){
            return ListView.builder(

              itemCount: value.newss.length,
              itemBuilder: (context, index) {
                return  Padding(padding: const EdgeInsets.symmetric(horizontal: 15.0,vertical: 5.0),
                child: Post(model: value.newss[index],),
                );
                
              },
            );
          }else{
            return const Center(child: Text("No News Avilable"),);
          }
        },

        ),
        
        )

HomeController Code

    class HomeController extends GetxController{
  final FirebaseFunctions _functions = FirebaseFunctions();
  final ScrollController controller =  ScrollController();
  List<NewsModel> newss = [];
  var isLoding = false.obs;
  void getData() async{
    newss.addAll(await _functions.getNews());
    update();
  }
  @override
  void onInit() {
    super.onInit();
    getData();
    controller.addListener(() {
      _functions.isLoading.listen((p) {
        isLoding.value = p;
      });
      double maxScrollPoint = controller.position.maxScrollExtent;
      double currentPosition = controller.position.pixels;
      double height20 = Get.size.height *0.20;
      if(maxScrollPoint - currentPosition <= height20){
        getData();
      }
    });
  }
}

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

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

发布评论

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

评论(2

凉城凉梦凉人心 2025-01-18 06:58:33

试试这个:

List<HomeController> homecontroller = value.newss[index]
child: Post(model: homecontroller)

Try this:

List<HomeController> homecontroller = value.newss[index]
child: Post(model: homecontroller)
伤痕我心 2025-01-18 06:58:33

我认为错误在于,因为您使用了扩展本身,所以您尝试将其删除,让我问您是否在行或列上使用了它

I think the error is that because You use the expanded itself did You try to remove it and let me ask did You use it on a row or column

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