Sorry for the big post in advance...
My app is a social network that behaves closely to Instagram.
PATTERN :
- User profile (with list of thumbnails, no videos played)
- Select photo (open a page with
Navigator.push()
with photos / playing videos - Select another user profile (in the video's comments for example)
- See again list of thumbnails, then posts with photos - playing videos, and so on...
This is like an infinite "profile - feed" loop. With such logic, i am reaching more or less 30 pages with Navigator.push() before i run into an OutOfMemory
error.
Flutter tools only says lost connection to device
but the more i use the Navigator
and the more laggy the app becomes and finally crashes, so i am 99% sure this is due to the memory usage.
This happens at 100% of the time, at 1 page difference more or less due to the scrolling in the post list.
The memory usage is increasing by 20MB for each page more or less if i don't scroll too much in the picture / video list.
I already plan to make my images smaller but this does just delay the issue at best.
QUESTIONS :
- Is it possible to never run into an
OutOfMemory
exception with this kind of "infinite pages" ? - I know that there is a
deactivate()
method inStateFul Widgets
that could be used afterNavigator.push
is called (dispose()
is not called because we don't remove anything from the tree), maybe some work should be done there ? - Should i do something to handle myself the
Navigator
stack ? I don't want topop()
old pages as i need to go back until the first one opened
In this situation and this logic it means that if i would go maybe through 100 pages in Instagram, this would also crash at 100%.
I am not sure anyone goes that far anyway and maybe that's what they are counting on... If there is no way to prevent the OutOfMemory
, the only solution might be to delay it until the user saw at least 100 pages for instance...
WORKAROUND that i have found in theory but not sure that's even possible in code :
The only solution i have think of until now is to allow the user to push()
a certain amount of pages, and keep a 20 pages maximum in Navigator
stack, but without removing the first page.
So there would always be 20 pages in memory.
If this is the only solution, can someone provide an example on how to deal with specific pages inside the Navigator
stack please ?
Also, i have noticed that the memory doesn't decrease when you come back with Navigator.pop()
, why ?
EDIT :
In order to display photos / videos, i am using essentially 2 packages
:
- flutter_staggered_grid_view : not the official package, but a pull request made it usable by disabling
automaticKeepAlives
. - scrollable_positioned_list : that works with a builder as well and with
automaticKeepAlives: false
.
I am also using Slivers in one part of the app but again with SliverStaggeredGrid.countBuilder
from the previously mentioned package.