Home > OS >  Flutter - duplicating an list of objects
Flutter - duplicating an list of objects

Time:10-27

I want to duplicate a list of objects. I want to do some things to to that dupliucate but not affect the original list.

Heres my code:

var tempHolidays = <Holiday>[];
tempHolidays = holidays;
tempHolidays[widget.index].start = widget.start;
tempHolidays[widget.index].end = widget.end;

The results i'm seeing would suggest the actions carried out on tempHolidays are mirroring on holidays? Is this possible or do I have a bug elsewhere?

CodePudding user response:

What you are doing is just passing the references of the initial list to the second.

To duplicate a list use toList(), it return a different List object with the same element of the original list

tempHolidays = holidays.toList()

  • Related