1

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?

1
  • Post the content of your pages (A & B) Commented Dec 20, 2020 at 20:45

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

7 Comments

I am using seState. I think the peoblem 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?
Yes, flutter doesn't compare 2 objects well. Equitable is a nice comparison package. It uses value equality instead of instance equality.
Is that gonna apply for this case: I want to compare two widgets not objects?
That, I'm unsure of. I never had to compare widgets. Although, widgets are objects.
Okay actually I have two list of objects I pass one of them from screen (a) and initially it is empty. The other one is created in screen b and it has some objects. What i am trying to do is comparing if Object from b exist in a and if yes i remove it and if no i add it.Every thing works perfectly except that when i pop from b to a. After popping and navigating again to screen b the list (a) is passed and with recent updates but when i tap on object that is already there the method for removing it does not work however, it was working before popping.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.