Home > Enterprise >  RangeError (index): Invalid value: Only valid value is 0: -1 while getting data
RangeError (index): Invalid value: Only valid value is 0: -1 while getting data

Time:10-26

I am getting data from the APIs and storing it in the model and displaying it in ProductCardList data but it is showing a range error I dont understand what is the range error while data is received correctly but did not show due to range error if anyone knows about it please let me know thanks in advance

enter image description here

 Expanded(
            child: GridView.builder(
              padding: EdgeInsets.symmetric(horizontal: isMobile(context)? 15.w:10.w, vertical: 8.h),
              itemCount: campaignDetailsModel!.data!.products!.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: isMobile(context)? 2:3,
                childAspectRatio: .75,
                mainAxisSpacing: isMobile(context)? 15:20,
                crossAxisSpacing: isMobile(context)? 15:20,
              ),
              itemBuilder: (context, index) {
              //   return Container(
              //     child:     Expanded(
              //   child: Padding(
              //     padding: EdgeInsets.all(8.r),
              //     child: Column(
              //       children: [
              //         Text("${campaignDetailsModel!.data!.products!}"),
                      
                      
              //       ],
              //     )
              //   ),
              // ),
              //   );
                return ProductCardList(
                  dataModel: campaignDetailsModel!.data!.products!,
                  index: index,
                );
              },
            ),
          ),

// ProductCardList class
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:ribbon_widget/ribbon_widget.dart';
import 'package:flutter/material.dart';
import 'package:yoori_ecommerce/src/utils/theme_manager.dart';
import '../_route/routes.dart';
import '../controllers/currency_converter_controller.dart';
import '../controllers/home_screen_controller.dart';
import '../utils/app_tags.dart';
import '../utils/app_theme_data.dart';
import '../utils/responsive.dart';

class ProductCardList extends StatefulWidget {
  const ProductCardList({
    Key? key,
    required this.dataModel,
    required this.index,
  }) : super(key: key);
  final dynamic dataModel;
  final int index;

  @override
  State<ProductCardList> createState() => _ProductCardListState();
}

class _ProductCardListState extends State<ProductCardList> {
  final currencyConverterController = Get.find<CurrencyConverterController>();

  @override
  Widget build(BuildContext context) {
    final homeController = Get.put(HomeScreenController());
    return Ribbon(
      farLength: widget.dataModel[widget.index].isNew!
          ? isMobile(context)
              ? 20
              : 30
          : 1,
      nearLength: widget.dataModel[widget.index].isNew!
          ? isMobile(context)
              ? 40
              : 50
          : 1,
      title: widget.dataModel[widget.index].isNew! ? AppTags.neW.tr : "",
      titleStyle: TextStyle(
        fontSize: isMobile(context) ? 10.sp : 7.sp,
        fontFamily: 'Poppins',
      ),
      color: AppThemeData.productBannerColor,
      location: RibbonLocation.topEnd,
      child: Container(
        height: 230.h,
        width: isMobile(context) ? 165.w : 125.w,
        decoration: BoxDecoration(
          color: ThemeController.isDark!
              ? AppThemeData.darkThemeColor
              : Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(7.r)),
          boxShadow: [
            BoxShadow(
              color: AppThemeData.boxShadowColor.withOpacity(0.1),
              spreadRadius: 0,
              blurRadius: 20.r,
              offset: const Offset(0, 10), // changes position of shadow
            ),
          ],
        ),
        child: InkWell(
          onTap: () {
            Get.toNamed(
              Routes.detailsPage,
              parameters: {
                'productId': widget.dataModel[widget.index].id!.toString(),
              },
            );
          },
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.all(5.r),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        widget.dataModel[widget.index].specialDiscountType == 'flat'
                            ? double.parse(widget.dataModel[widget.index].specialDiscount) ==
                                    0.000
                                ? const SizedBox()
                                : Container(
                                    height: 20.h,
                                    decoration: BoxDecoration(
                                      color: AppThemeData
                                          .productBoxDecorationColor
                                          .withOpacity(0.06),
                                      borderRadius: BorderRadius.all(
                                        Radius.circular(3.r),
                                      ),
                                    ),
                                    child: Center(
                                      child: Text(
                                        "${currencyConverterController.convertCurrency(widget.dataModel[widget.index].specialDiscount)} OFF",
                                        style: isMobile(context)
                                            ? AppThemeData.todayDealNewStyle
                                            : AppThemeData.todayDealNewStyleTab,
                                      ),
                                    ),
                                  )
                            : widget.dataModel[widget.index].specialDiscountType ==
                                    'percentage'
                                ? double.parse(
                                            widget.dataModel[widget.index].specialDiscount) ==
                                        0.000
                                    ? const SizedBox()
                                    : Container(
                                        height: 20.h,
                                        decoration: BoxDecoration(
                                          color: AppThemeData
                                              .productBoxDecorationColor
                                              .withOpacity(0.06),
                                          borderRadius: BorderRadius.all(
                                            Radius.circular(3.r),
                                          ),
                                        ),
                                        child: Center(
                                          child: Text(
                                            "${homeController.removeTrailingZeros(widget.dataModel[widget.index].specialDiscount)}% OFF",
                                            textAlign: TextAlign.center,
                                            style: isMobile(context)
                                                ? AppThemeData.todayDealNewStyle
                                                : AppThemeData
                                                    .todayDealNewStyleTab,
                                          ),
                                        ),
                                      )
                                : Container(),
                      ],
                    ),
                    double.parse(widget.dataModel[widget.index].specialDiscount) == 0.000
                        ? const SizedBox()
                        : SizedBox(width: 5.w),
                    widget.dataModel[widget.index].currentStock == 0
                        ? Container(
                            height: 20.h,
                            decoration: BoxDecoration(
                              color: AppThemeData.productBoxDecorationColor
                                  .withOpacity(0.06),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(3.r)),
                            ),
                            child: Center(
                              child: Text(
                                AppTags.stockOut.tr,
                                style: isMobile(context)
                                    ? AppThemeData.todayDealNewStyle
                                    : AppThemeData.todayDealNewStyleTab,
                              ),
                            ),
                          )
                        : const SizedBox(),
                  ],
                ),
              ),
              SizedBox(
                height: 18.h,
              ),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.all(8.r),
                  child: Image.network(
                    widget.dataModel[widget.index].image!,
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              SizedBox(height: 14.h),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 7.w),
                child: Text(
                  widget.dataModel[widget.index].title!,
                  maxLines: 1,
                  textAlign: TextAlign.center,
                  style: isMobile(context)
                      ? AppThemeData.todayDealTitleStyle
                      : AppThemeData.todayDealTitleStyleTab,
                ),
              ),
              SizedBox(height: 5.h),
              Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: isMobile(context) ? 18.w : 10.w),
                child: Center(
                  child: double.parse(widget.dataModel[widget.index].specialDiscount) == 0.000
                      ? Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              currencyConverterController
                                  .convertCurrency(widget.dataModel[widget.index].price!),
                              style: isMobile(context)
                                  ? AppThemeData.todayDealDiscountPriceStyle
                                  : AppThemeData.todayDealDiscountPriceStyleTab,
                            ),
                          ],
                        )
                      : Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              currencyConverterController
                                  .convertCurrency(widget.dataModel[widget.index].price!),
                              style: isMobile(context)
                                  ? AppThemeData.todayDealOriginalPriceStyle
                                  : AppThemeData.todayDealOriginalPriceStyleTab,
                            ),
                            Text(
                              currencyConverterController.convertCurrency(
                                  widget.dataModel[widget.index].discountPrice!),
                              style: isMobile(context)
                                  ? AppThemeData.todayDealDiscountPriceStyle
                                  : AppThemeData.todayDealDiscountPriceStyleTab,
                            ),
                          ],
                        ),
                ),
              ),
              SizedBox(
                height: 5.h,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

The issue is with your currency calculator, you were getting invalid value of price range which was generating null value and index was unable to load that null value. This issue is resolved

  • Related