Saeed
Saeed

Reputation: 95

Adding data from one List of Widgets to another List in Flutter

So I have a widget (a) which contains an animated list and an empty list of widgets (SelectedWorkoutsList). Then after tapping any of the widgets in the list, I navigate to another widget (b) and pass the SelectedWorkoutsList which is still empty using Navigator. meanwhile in widget (b) i have the SelectedWorkoutsList) which is still empty and another List(WorkoutObjects) which contains widgets. After selecting any of its widgets the widget should be added to the SelectedWorkoutsList passed from Widget (a) and if the user tapped another time on the same widget it should be removed. All of this work perfectly, my problem is that after adding the widgets and pop back to the first widget (a) and then Navigate to (b) I still have that SelectedWorkoutsList but if I select a widget which I already selected before popping, it will add it as a duplicate while it should remove it.

I am using this if condition to check whether that widget was a already selected or not.

onTap: () {
                        if(!selectedWorkoutsList.contains(workoutObjects[index])) {
                          selectedWorkoutsList.add(workoutObjects[index]);
                        }else{
                          selectedWorkoutsList.remove(workoutObjects[index]);
                        }
                        print(selectedWorkoutsList.length);
                      },

So i have the workoutObjects List which contains Widgets and onTap, i check if the selectedWorkoutsList contains the widget that the user tapped from workoutObjects is yes it is removed is no it is added. it works fine except when I pop back then navigate again.

I am using seState. I think the problem is related to HashCode of the objects in the list (workoutsObject). Every time I load the widget the workoutObject are recreated in the init state with new hashcodes so when I compare widget with another widget in the selectedList it doesn't recognize that they are the same because of the difference in hashcodes. Is there a way to override the hashcode and compare two widgets using different method?

Upvotes: 1

Views: 863

Answers (1)

Scott Godfrey
Scott Godfrey

Reputation: 671

The problem here, is when you pop a page off, the previous page still has it's previous state. So, anything removed from the selected list will still be there on old page. What state management system are you using?

Upvotes: 1

Related Questions