So, I have been trying to implement a widget (a container with a GestureDetector) so that when the user clicks (onTap(s) or presses) on the container, it will initiate a Rive animation using a custom .riv
file.
I have done the initialization of Rive at main.dart
where the application starts.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await RiveFile.initialize();
runApp(const MainApp());
}
I have also implemented a try-catch function to detect any potential errors during the loading of the data inside that file and to make everything clear (since this was the first time dealing with a .riv file on Flutter) I have added a condition that detects that the data has been loaded and that it was not null. That condition is a ternary operator if the result is null, it would return a Text() instead of the normal Rive().
However, for some reason the data that loaded was null
????.
That resulted in the other Text() widget to of course appear instead of the actual Rive File.
I tried initiating the load at the beginning of the file, so that I can check for errors and catch them before the application itself runs, I tried using print()
statements to checks for why the value is null by following the code as follows:
Future<void> _loadRiveFile() async {
try {
final data = await rootBundle.load('assets/rives/custom_menu_button.riv');
print("Data: $data");
final file = RiveFile.import(data);
print("File: $file");
final artboard = file.mainArtboard;
print("Artboard: $artboard");
var controller =
StateMachineController.fromArtboard(artboard, 'State Machine');
print("Controller: $controller");
if (controller != null) {
artboard.addController(controller);
_toggleTrigger = controller.findInput('open') as SMITrigger;
_isButtonActive = _toggleTrigger?.value ?? false;
setState(() {
_artboard = artboard;
});
} else {
print("Error loading");
}
} catch (e) {
print("Error during Rive file loading: $e");
}
}
And the output was
I/flutter ( 5343): Data: _UnmodifiableByteDataView
I/flutter ( 5343): File: Instance of 'RiveFile'
I/flutter ( 5343): Artboard: Instance of 'RuntimeArtboard' (0) -> Artboard
I/flutter ( 5343): Controller: Instance of 'StateMachineController'
I/flutter ( 5343): Error during Rive file loading: type 'Null' is not a subtype of type 'SMITrigger' in type cast