CSV data not showing up in my flutter app screen

I am trying to build a myntra fashion app in flutter using a csv dataset. This is my code:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:csv/csv.dart';
import 'package:flutter/services.dart' show rootBundle;
// ignore: unused_import
import 'product.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<Product> _products = [];
  List<Product> _filteredProducts = [];
  bool _isLoading = true;

  @override
  void initState() {
    super.initState();
    _loadCsvData();
  }

  Future<void> _loadCsvData() async {
    try {
      final csvString =
          await rootBundle.loadString('assets/myntra_fashion6.csv');
      List<List<dynamic>> csvTable = CsvToListConverter().convert(csvString);

      if (csvTable.isNotEmpty) {
        print('CSV Table Headers: ${csvTable[0]}');
      }

      List<Product> products = [];
      for (var i = 1; i < csvTable.length; i++) {
        print('CSV Row $i: ${csvTable[i]}');
        products.add(Product.fromCsv(csvTable[i]));
      }

      setState(() {
        _products = products;
        _filteredProducts = products;
        _isLoading = false;
      });
    } catch (e) {
      print("Error loading CSV data: $e");
    }
  }

  void _filterProducts(String query) {
    List<Product> filtered = _products.where((product) {
      return product.brandName.toLowerCase().contains(query.toLowerCase()) ||
          product.description.toLowerCase().contains(query.toLowerCase()) ||
          product.category.toLowerCase().contains(query.toLowerCase());
    }).toList();

    setState(() {
      _filteredProducts = filtered;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Myntra Fashion'),
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(kToolbarHeight),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              onChanged: (value) {
                _filterProducts(value);
              },
              decoration: InputDecoration(
                hintText: 'Search...',
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8.0),
                  borderSide: BorderSide.none,
                ),
                filled: true,
                contentPadding: EdgeInsets.all(16.0),
                fillColor: Colors.white,
              ),
            ),
          ),
        ),
      ),
      body: _isLoading
          ? Center(child: CircularProgressIndicator())
          : ListView.builder(
              itemCount: _filteredProducts.length,
              itemBuilder: (context, index) {
                return ProductCard(product: _filteredProducts[index]);
              },
            ),
    );
  }
}

class ProductCard extends StatelessWidget {
  final Product product;

  ProductCard({required this.product});

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.all(8.0),
      child: ListTile(
        leading: Image.network(product.url,
            width: 50, height: 50, fit: BoxFit.cover),
        title: Text(product.brandName),
        subtitle: Text(product.description),
        trailing: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('₹${product.discountPrice.toStringAsFixed(2)}',
                style: TextStyle(fontWeight: FontWeight.bold)),
            Text('₹${product.originalPrice.toStringAsFixed(2)}',
                style: TextStyle(decoration: TextDecoration.lineThrough)),
          ],
        ),
      ),
    );
  }
}

I tried several debugging steps but the screen still keeps getting stuck at circular loading indicator. This is the product model:

class Product {
  final String url;
  final int productId;
  final String brandName;
  final String category;
  final String individualCategory;
  final String categoryByGender;
  final String description;
  final double discountPrice;
  final double originalPrice;
  final int discountOffer;
  final String sizeOption;
  final double ratings;
  final int reviews;
  final String standardizedSize;

  Product({
    required this.url,
    required this.productId,
    required this.brandName,
    required this.category,
    required this.individualCategory,
    required this.categoryByGender,
    required this.description,
    required this.discountPrice,
    required this.originalPrice,
    required this.discountOffer,
    required this.sizeOption,
    required this.ratings,
    required this.reviews,
    required this.standardizedSize,
  });

  factory Product.fromCsv(List<dynamic> csvRow) {
    return Product(
      url: csvRow[0],
      productId: int.parse(csvRow[1]),
      brandName: csvRow[2],
      category: csvRow[3],
      individualCategory: csvRow[4],
      categoryByGender: csvRow[5],
      description: csvRow[6],
      discountPrice: double.parse(csvRow[7]),
      originalPrice: double.parse(csvRow[8]),
      discountOffer: int.parse(csvRow[9]),
      sizeOption: csvRow[10],
      ratings: double.parse(csvRow[11]),
      reviews: int.parse(csvRow[12]),
      standardizedSize: csvRow[13],
    );
  }
}

what should i do in order to correctly display the csv content in the app?

i tried to debug the code using print statements and i got this error:
Error loading CSV data: RangeError (index): Index out of range: index should be less than 1: 1

New contributor

Riya Birnale is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật