Flutter: Flaky Dynamic Images
Hi there, I’m upgrading our implementation to the latest Flutter pre-release to support our incoming design requirements:
https://pub.dev/packages/rive/versions/0.14.0-dev.6
When providing images at runtime they do not always display. We have several instances of the same Rive file on one screen, using different artboards and machines.
The first artboard displays images correctly most of the time, while another instance with the same configuration further down the page shows images correctly half of the time. Other artboards show no images at all, despite them being provided through the same mechanisms.
I initially thought this was due to use of the older asset loader API, but the symptoms seem identical when switching to the new view model image API.
The images, and how they are provided to the Rive file, currently work using 0.13.20 and below.
I am happy to provide videos and .rev files via email etc.
Here is a simplified version of the code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rive/rive.dart';
class RiveAnimationWidget extends StatefulWidget {
const RiveAnimationWidget({
required this.src,
required this.artboard,
required this.machine,
this.images = const {},
super.key,
});
final String src;
final String artboard;
final String machine;
final Map<String, Uint8List> images;
@override
createState() => _RiveAnimationWidget();
}
class _RiveAnimationWidget extends State<RiveAnimationWidget> {
/// The rive resource loaded from disk with images injected
File? _file;
/// The rive animation controller
RiveWidgetController? _controller;
@override
dispose() {
_file?.dispose();
_controller?.dispose();
super.dispose();
}
@override
build(context) {
// Load the file if it hasn't been loaded yet
if (_controller == null) {
unawaited(_initRive());
// Wait for the file to load
return SizedBox.shrink();
}
// Return the animation
return RiveWidget(controller: _controller!);
}
/// Load the rive file and initialise the controller
Future<void> _initRive() async {
_file ??= await File.path(widget.src, riveFactory: Factory.rive);
// Initialise the controller with the file
_controller ??= RiveWidgetController(
_file!,
artboardSelector: ArtboardSelector.byName(widget.artboard),
stateMachineSelector: StateMachineSelector.byName(widget.machine),
);
// Get the view model to update
final vm = _file?.viewModelByName('Main Big');
final vmi = vm?.createDefaultInstance();
if (vmi != null) {
// Loop through all provided images
for (var asset in widget.images.entries) {
// Get the image property
final prop = vmi.image(asset.key);
if (prop == null) continue;
// Create a RenderImage
final image = await Factory.rive.decodeImage(asset.value);
if (image == null) continue;
// If the image is valid, update the image property value
prop.value = image;
}
// Bind the view model instance to the state machine
_controller?.stateMachine.bindViewModelInstance(vmi);
}
// Rebuild the component
if (mounted) setState(() {});
}
}