I have a Futurebuilder that fetches data from an Api. I am assigning its data to a list;
List<TicketModel> ticketList = [];`
ticketList = snapshot.data;
Now the weird thing is, snapshot.data and ticketList should be independent right?
However if I remove data from the List e.g I remove every Object that has the price 1 the snapshot.data modifies too(Has same data as list now).
Why is that so?
The Futurebuilder is in a Modal bottom sheet if that's necessary
void main() {
List<String> listofAllTickets =[];
listofAllTickets = ["ticket eins","ticket zwei", "ticket drei"];
List<String> listOfAllTicketsSecond = listofAllTickets;
listOfAllTicketsSecond.removeWhere((element) => element == "ticket eins");
print(listofAllTickets);
print(listOfAllTicketsSecond);
}
CodePudding user response:
you need to use a futurebuilder and connect ticketlist to it. the futurebuilder will generate a snapshot in its callback function. you can not access the snapshot outside of the futurebuilder.
I prefer using Bloc and streambuilder. Streambuilder events occur when change occurs on the notification stream.
CodePudding user response:
As @Jeppe posted in the comments what I did before was that:
void main() {
List<String> listofAllTickets =[];
listofAllTickets = ["ticket eins","ticket zwei", "ticket drei"];
List<String> listOfAllTicketsSecond = listofAllTickets;
listOfAllTicketsSecond.removeWhere((element) => element == "ticket eins");
print(listofAllTickets);
print(listOfAllTicketsSecond);
}
the right solution would be doing this:
void main() {
List listofAllTickets = ["ticket eins","ticket zwei", "ticket drei"];
List listOfAllTicketsSecond = [];
listOfAllTicketsSecond = List.from(listofAllTickets);
listOfAllTicketsSecond.removeWhere((element) => element == "ticket eins");
print(listofAllTickets);
print(listOfAllTicketsSecond);
}
In Example two I am copying the data instead of referencing the list.