'Flutter RangeError while when comparing two list

Hello I have 2 list and I want to use these in ListViewBuilder.

List's:

List times = ['08:30', '09:00', '09:30', '10:00', '13:00'];
List obj = [true,false,true];

I tried this:

ListView.builder(
                controller: scrollController,
                shrinkWrap: true,
                itemCount: times.length,
                itemBuilder: (BuildContext context, int index) {
                  return Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: InkWell(
                      onTap: () {
                        setState(() {
                          selected = index;
                          debugPrint(tarih[index]);
                        });
                      },
                      child: Container(
                        color: obj[index] ? Colors.yellow : Colors.red,
                        height: 30,
                        width: 12,
                        child: Text(times[index]),
                      ),
                    ),
                  );
                },
              ),

Here is the error:

enter image description here

I know what cause's this error. Because of obj.length does not match with times.length

But I still want to create the other Containers. How do I solve this?



Solution 1:[1]

Well there might be other ways to do so, what I did was, just to copy the obj list items as many as there are items in times list in order. And if the number is not equal just add remaining number at last.

 List times = [
'08:30',
'09:00',
'09:30',
'10:00',
'13:00',
'13:00',
'13:00',
'13:00',
'13:00',
];

List obj = [true, false, true];

@override
Widget build(BuildContext context) {
// Remaining length of times list after we copy
int remaining = times.length % obj.length;
//Exact Number of times to copy obj
int exactNumber = (times.length - remaining) ~/ obj.length;
List shallowList = [];
// Using for loop copy the list as many as exactNumber
for (int i = 0; i < exactNumber; i++) {
  shallowList += obj;
}
// Add add remaining subList 
// Then we have obj list with same length as times
List finalShallowList = [...shallowList, ...obj.sublist(0, remaining)];
// Create Separate Index for obj that we can reset
return Scaffold(
  body: Container(
    child: ListView.builder(
      shrinkWrap: true,
      itemCount: times.length,
      itemBuilder: (BuildContext context, int index) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: InkWell(
            onTap: () {},
            child: Container(
               // Loop over finalShallowList instead of obj
              color: finalShallowList[index] ? Colors.yellow : Colors.red,
              height: 30,
              width: 12,
              child: Text(times[index]),
            ),
          ),
        );
      },
    ),
  ),
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nibukdk93