0

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 Navigateed 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? )

Sam Ramezanli
  • 998
  • 9
  • 20
  • Hello did you found a solution ? i'm stuck with the same problem... – Jeremy Dormevil Apr 20 '22 at 08:26
  • the fix was totally unrelated to all of these. I'm using `flutter_bloc` in my project and I was instantiating all of my `BlocProvider` for all classes at the very beginning (inside `main()`). I then moved my BlocProvider inside the `CupertinoPageRoute`'s `builder` in `Navigator.push()`. This way, each BlocProvider is limited for that particular class only. This was the fix. – Sam Ramezanli Apr 20 '22 at 14:41

2 Answers2

0

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

Mohammed Ali
  • 1,117
  • 1
  • 16
  • 31
  • I don't think this issue is architecture-related. Thie problem is that each time I Navigate to a new `Apartment` class, the `build()` function of all the existing `Apartment` classes in Stack, gets called. I only wish the `build()` method of the new `Apartment` page gets called. – Sam Ramezanli Oct 16 '20 at 23:04
0

I'm not sure if I got your question correctly, but if you want to check something is the same as the previous object for not creating new one then "equatable" may help. you'r objects will extend from "equatable".

but in your case when you will navigate to new widget even if it's the same one that you are in it's normal to create new instance of it.

if you can give me the big picture that would be great...

Baraa Aljabban
  • 992
  • 13
  • 22