We have a PageView
(technically a PreloadPageView
) that contains videos (they can be large). The way we are currently handling the playing/pausing is by having a separate GlobalKey()
for each of them and calling globalKey.currentState.play()/.pause()
. Now this is working fine, but since the GlobalKey
s hold the references to the states of these videos, it never lets Flutter dispose these states, which leads to an OutOfMemory
error.
We already tried to manually call globalKey.currentState.dispose()
, the problem with that is we need to decide how many videos we keep in memory, which should be different depending on how large the videos are (some of them are using DASH, so only part of them are loaded, some of them are full videos) and it should preferably also be different for devices with different amount of free memory.
Ideally, Flutter should keep track of memory usage and should clear the pages from memory if there are too many, so the best solution would be letting it clear pages, but we also need to be able to play and pause those videos. One solution would be not using GlobalKey
s and using props instead (e.g., to pass isPlaying
prop to the video), however, it doesn't seem to be a good practice to mix declarative and imperative programming in this case and it would also make the code less readable.
Is there any way to still have a reference to the state (to be able to call play/pause imperatively), but let Flutter clear it from memory to avoid OOM errors?