Stumped and Scratching my head over this error any ideas?

Viewed 54

I'm Working on a ecommerce flutter program that involves motorcycle parts, until I stumbled upon an error that I am stuck with and was wondering if you guys got any idea why. Here are the code snippets From the API to the Front-end.

 The Error:
    RangeError(index): Index out of range: index should be less than 1:1


//This where I call the API through this function 

Future<List<Welcome>> fetchData() async {
 final response = await http.get(Uri.parse(orders));
 var body = response.body;

 List<Welcome> wel = List<Welcome>.from(
    (json.decode(body) as List).map((x) => Welcome.fromJson(x)));

 return wel;
}


//Heres Where I display my data using FutureBuilder  

FutureBuilder<dynamic>(
        future: CartItemAPI().fetchData(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return ListView.builder(
              shrinkWrap: true,
              itemCount: snapshot.data!.length,
              itemBuilder: (context, index) {
                return Padding(
                  padding: EdgeInsets.all(10),
                  child: Container(
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: Colors.black26,
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: Row(
                      children: <Widget>[
                        Image(
                          width: 200,
                          height: 150,
                          image: NetworkImage(
                              "https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/pile-of-tires-on-white-background-royalty-free-image-672151801-1561751929.jpg?crop=1.00xw:0.629xh;0,0.318xh&resize=480:*"),
                        ),
                        Column(
                          children: <Widget>[
                            Text(
                              snapshot
                                  .data![index].spareparts[index].warehouse,
                              style: TextStyle(
                                color: Colors.black38,
                                fontSize: 20,
                              ),
                            ),
                            SizedBox(height: 10),
                            Text(
                              snapshot.data![index].spareparts[index].name,
                              style: TextStyle(
                                color: Colors.black38,
                                fontSize: 20,
                              ),
                            ),
                            SizedBox(height: 10),
                            Row(
                              children: [
                                Checkbox(
                                  checkColor: Colors.white,
                                  value: ischecked,
                                  onChanged: (bool? value) {
                                    setState(() {
                                      ischecked = value!;
                                    });
                                  },
                                ),
                              ],
                            )
                          ],
                        ),

Heres the API I was provided..

[ { "id": "f0507ec1-1b14-440f-b6a5-036535608172", "created": "2022-9-1", "spareparts": [ { "id": "20c9436f-c2b3-4cdb-b836-d853152a8e2c", "name": "Albert", "sku": "sku", "warehouse": "Skygo", "quantity": 4 }
     {
       "id": "f0507ec1-1b14-440f-b6a5-036535608172",
       "created": "2022-9-1",
       "spareparts": [
       {
        "id": "20c9436f-c2b3-4cdb-b836-d853152a8e2c",
        "name": "Albert",
        "sku": "sku",
        "warehouse": "Skygo",
        "quantity": 4
    }
]

Heres the Model class I used to translate the API for me.

class Welcome {
  Welcome({
    required this.id,
    required this.created,
    required this.spareparts,
 });

 String id;
 String created;
 List<Sparepart> spareparts;

  factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
    id: json["id"],
    created: json["created"],
    spareparts: List<Sparepart>.from(
        json["spareparts"].map((x) => Sparepart.fromJson(x))),
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "created": created,
    "spareparts": List<dynamic>.from(spareparts.map((x) => x.toJson())),
  };
 }

class Sparepart {
   Sparepart({
    required this.id,
    required this.name,
    required this.warehouse,
    required this.quantity,
  });

  String id;
  String name;
  String warehouse;
  int quantity;

  factory Sparepart.fromJson(Map<String, dynamic> json) => Sparepart(
    id: json["id"],
    name: json["name"],
    warehouse: json["warehouse"],
    quantity: json["quantity"],
  );

    Map<String, dynamic> toJson() => {

    "id": id,
    "name": name,
    "warehouse": warehouse,
    "quantity": quantity,
  };

 }

So yeah..I've been spending hours trying to solve this code problem and asked a few but none of them know why either. And I was wondering if you guys would know why this error shows up and thank you in advanced for taking time helping me out. Really appreciate it. (First time poster here).

0 Answers
Related