1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter Best Practices: Refactoring dynamic-list state in multi-page app

Discussão em 'Mobile' iniciado por Stack, Outubro 7, 2024 às 13:23.

  1. Stack

    Stack Membro Participativo

    I have 3 BottomNavigationBar tabs managed using GoRouter. On the Home page, I store the current dynamic list state of the home page- e.g. ValueNotifier homeState = useState([Object1, Object2, Object3]).

    From Home, I have a button that routes to a subpage, say Subhome with parameters obj and onUpdate which are used to modify the state of Home. I.e. I pass Object1.copy() in, which is used to initialize a state in Subhome, say objState. This state is then modified in Subhome and in Subsubhome, etc. It needs to be stateful in this way so that on backpress from Subsubhome, we don't need to pass the state back up, it is already on the page (this is why I'm not passing in the original homeState as a stateful parameter).

    On backpress from Subhome, onUpdate is called which updates homeState with the changes to Object1. The issue is that with BottomNavigationBar, pressing a tab while already on that tab resets you to the first page of that tab, which skips the onUpdate call. An easy workaround was to just check if we're already on the same tab before navigating, but this is obviously not an ideal solution.

    The implementation in general seems a bit clunky tbh, so I'm hoping for some insight into how an experienced Flutter dev might approach the situation. Thanks for reading, any thoughts are much appreciated. Had to oversimplify a bit to keep this as short as I can, so happy to clarify on any points.

    Continue reading...

Compartilhe esta Página