.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 RiveFile
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, useFile.url('YOUR:URL')
. For network assets, cache the file in memory to avoid repeated downloads and unnecessary decoding of the file.