Caching a Rive File
Under most circumstances a .riv
file should load quickly and managing the RiveFile
yourself is not necessary. But if you intend to use the same .riv
file in multiple parts of your application, or even on the same screen, it might be advantageous to load the file once and keep it in memory.
Example Usage
The following is a basic example to illustrate how to preload a Rive file, and pass the data directly to the RiveAnimation.direct()
constructor:
Other Considerations
Managing State
How the RiveFile
is kept alive in state and shared to other widgets is up to you and your preferred state management solution. One approach can be to wait for the Rive file to load in main
, or during the startup screen, and using the Provider package to expose the data to the whole application.
Or if the animation is only needed in a nested section of your application, then it might be preferable to delay loading the animation until necessary.
Memory
By managing the RiveFile yourself you can have finer control over the memory used in your application. This will be especially beneficial if the same Rive file is being used in multiple parts of your application, or used simultaneously in multiple RiveAnimation
widgets.
You can make use of Flutter DevTools’ memory tooling for additional investigation if needed or desired.
Network Assets
The easiest way to load a Rive animation over the Internet is by using RiveAnimation.network(url)
. However, similar considerations apply to a network asset regarding memory and sharing a Rive file across multiple widgets/pages.
The following can be used to load a Rive file over the network :
The reference can then be kept alive in memory and the riveFile
can be passed to RiveAnimation.direct
.
The following is a basic example to illustrate how to preload a Rive file, and pass the data directly to the RiveAnimation.direct()
constructor:
Other Considerations
Managing State
How the RiveFile
is kept alive in state and shared to other widgets is up to you and your preferred state management solution. One approach can be to wait for the Rive file to load in main
, or during the startup screen, and using the Provider package to expose the data to the whole application.
Or if the animation is only needed in a nested section of your application, then it might be preferable to delay loading the animation until necessary.
Memory
By managing the RiveFile yourself you can have finer control over the memory used in your application. This will be especially beneficial if the same Rive file is being used in multiple parts of your application, or used simultaneously in multiple RiveAnimation
widgets.
You can make use of Flutter DevTools’ memory tooling for additional investigation if needed or desired.
Network Assets
The easiest way to load a Rive animation over the Internet is by using RiveAnimation.network(url)
. However, similar considerations apply to a network asset regarding memory and sharing a Rive file across multiple widgets/pages.
The following can be used to load a Rive file over the network :
The reference can then be kept alive in memory and the riveFile
can be passed to RiveAnimation.direct
.
Here’s a simplified example showing how to integrate the useRiveFile
hook to reuse a RiveFile
across components
Not yet supported
The following is a basic example to illustrate how to preload a Rive file, and pass the data to multiple Rive instances:
When using the RiveViewModel(fileName:)
initializer, the Apple runtime does not cache file usage; that has to be handled manually. You may find that when reusing the same file, your memory usage increases (over time) as you create more view models. This is when you should consider caching the underlying file for reuse.
Reusing a single RiveFile
(when applicable) will reduce the overall memory usage of your application. If your .riv
can be reused across multiple views, where each view requires the same file but uses different artboards or state machines, consider caching the RiveFile
before creating your view models. While one RiveFile
can be cached, to ensure that each view is in its own state, you must create a unique RiveModel
per RiveViewModel
instance.
Was this page helpful?