(Flutter Runtime) Rive Events Not Triggering with Cached Artboard
Hey everyone,
I'm using Rive in Flutter (iOS & Android) and preloading my Rive files using a cache system with Provider. The animations load fine, but I'm facing an issue with Rive events not triggering when using a cached Artboard and touching the phone screen.
I have prior coding experience, but still a bit new using Flutter and Dart.
The caching is done as explained here:
Issue
When using RiveAnimation.asset(), event listeners work as expected:
RiveAnimation.asset(
'assets/rive/animation.riv',
fit: BoxFit.contain,
onInit: _onInit,
);However, when I load the same animation from my cache and use:
Rive(artboard: riveProvider.artboard!)The event listener (controller.addEventListener(_onRiveEvent)) does not trigger for some reason.
What I've Tried
Debugging _onRiveEvent, but it never gets called.
Initializing the .addEventListener both in the provider and in the widget
Run the problem in —profile and —no-enable-impeller
Checking if the controller exists when adding the event listener.
Using SchedulerBinding.instance!.addPostFrameCallback to ensure events are added after the frame builds.
Provider code
Here’s the provider code that doesn’t work
class RiveProvider with ChangeNotifier {
StateMachineController? _controller;
Artboard? _artboard;
Artboard? get artboard => _artboard;
StateMachineController? get controller => _controller;
Future<void> _loadAnimation() async {
final ByteData data = await rootBundle.load('assets/rive/animation.riv');
final RiveFile riveFile = RiveFile.import(data);
final Artboard artboard = riveFile.mainArtboard;
_controller = StateMachineController.fromArtboard(artboard, 'state');
if(_controller != null) {
artboard.addController(_controller!);
_controller?.addEventListener(_onRiveEvent);
}
_artboard = artboard;
notifyListeners();
}
}Working code without provider
Here’s the code where events work
void _onInit(rive.Artboard artboard) {
var ctrl = rive.StateMachineController.fromArtboard(artboard, 'state')!;
_controller = ctrl;
ctrl.addEventListener(_onRiveEvent);
artboard.addController(ctrl);
}And here’s the _onRiveEvent that is in both
void _onRiveEvent(RiveEvent event) {
print("Event triggered!");
}Do I actually need a cached system?
Not necessarily. I want the rive animation loading times to be as smooth as possible, and currently the solution without the cache gives a lag when the widget is loaded.
Has anyone encountered this before? How do I properly add event listeners to a cached Artboard?
Thanks in advance! 🙏