I'm dealing with a scenario where I have an Apartment
page (StatefulWidget
) and on this page, I also have a list of "similar apartments".
When the user clicks on any of the similar apartments, they are Navigate
ed to a different instance of Apartment
class, but with new content.
However, I realized with logs that each time I Navigate to the new Apartment
page, all the existing instances of the Apartment
class on the Stack, are also getting recreated and it's causing issues.
Here is the code I use to Navigate to a new instance:
Navigator.push(
context,
CupertinoPageRoute(
builder: (context) => AptPage(
key: ObjectKey("$buildingId$unitNum"),
unitNumber: unitNum,
buildingId: buildingId,
cubit: BlocProvider.of<BuildingInfoCubit>(context)),
),
);
What I wish is that previously created Apartment
pages, do not get recreated again, and also I still want to be able to go back to previous Apartment
pages on the Stack.
(Am I using ObjectKey wrong? )
on this situation , and in the short answer you need to change the whole process when dealing with variables gotten from api or what ever, you need to use this MVC plugin to save information and variables to the Controller class and implement the controller by the widgets so you can get the variables as you wish in an infinity number of widgets , or if you found the change to MVC pattern is difficult you can use the
notifiable
variables and assign it as public through the app when you use the variables, so get the data from the variable it self and notify when it is updated, so not to reload it on every widget call ,i do not recommend sending data between widgets as route arguments