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
In Flutter, you are responsible for managing the lifecycle of a Rive file. You can create a File
object directly, or use the FileLoader
convenience class with RiveWidgetBuilder
. In both cases, you must call dispose()
on the object when it’s no longer needed to free up memory.
To optimize memory usage, reuse the same File
object across multiple RiveWidget
instances if they use the same .riv
file. This ensures the file is loaded only once and shared in memory.
After a File
is disposed, it cannot be used again. To use the same .riv
file, create a new File
object.
Managing State
How you keep the Rive File
alive and share it with widgets depends on your state management approach. For global access, load the file in main
or during app startup, and expose it using a package like Provider. If the file is only needed in a specific part of your app, consider loading the file only when required.
Memory
Managing the file yourself gives you fine-grained control over memory usage, especially when the same Rive file is used in multiple places or simultaneously in several widgets. Use Flutter DevTools memory tooling to monitor and optimize memory if needed.
Network Assets
To load a Rive file from the Internet, use File.url('YOUR:URL')
. For network assets, cache the file in memory to avoid repeated downloads and unnecessary decoding of the file.
In Flutter, you are responsible for managing the lifecycle of a Rive file. You can create a File
object directly, or use the FileLoader
convenience class with RiveWidgetBuilder
. In both cases, you must call dispose()
on the object when it’s no longer needed to free up memory.
To optimize memory usage, reuse the same File
object across multiple RiveWidget
instances if they use the same .riv
file. This ensures the file is loaded only once and shared in memory.
After a File
is disposed, it cannot be used again. To use the same .riv
file, create a new File
object.
Managing State
How you keep the Rive File
alive and share it with widgets depends on your state management approach. For global access, load the file in main
or during app startup, and expose it using a package like Provider. If the file is only needed in a specific part of your app, consider loading the file only when required.
Memory
Managing the file yourself gives you fine-grained control over memory usage, especially when the same Rive file is used in multiple places or simultaneously in several widgets. Use Flutter DevTools memory tooling to monitor and optimize memory if needed.
Network Assets
To load a Rive file from the Internet, use File.url('YOUR:URL')
. For network assets, cache the file in memory to avoid repeated downloads and unnecessary decoding of the file.
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.