rive-app / rive-flutter

Flutter runtime for Rive
https://rive.app
MIT License
1.21k stars 191 forks source link

Error while using Rive [redraw called on null] #92

Closed NachiketaVadera closed 3 years ago

NachiketaVadera commented 3 years ago

To use rive in multiple places, I created a wrapper widget around the Rive widget.

//imports...
class RiveAnimation extends StatefulWidget {
  final String path;
  final String animation;
  final Alignment alignment;
  final bool useArtboardSize;
  RiveAnimation({
    @required this.path,
    this.animation = animTrigger,
    this.alignment = Alignment.center,
    this.useArtboardSize = true,
  });
  @override
  _RiveAnimationState createState() => _RiveAnimationState();
}

class _RiveAnimationState extends State<RiveAnimation> {
  Artboard _riveArtboard;
  RiveAnimationController _controller;

  @override
  void initState() {
    rootBundle.load(widget.path).then(
      (data) async {
        print('animation loaded');
        final RiveFile file = RiveFile.import(data);
        if (file != null) {
          final Artboard artboard = file.mainArtboard;
          artboard.addController(
            _controller = SimpleAnimation(widget.animation),
          );
          setState(() {
            _riveArtboard = artboard;
          });
        }
      },
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Rive(
      artboard: _riveArtboard,
      alignment: widget.alignment,
      useArtboardSize: widget.useArtboardSize,
      fit: BoxFit.fill,
    );
  }
}

While using this widget, I get an error saying:

I/flutter ( 7139): The getter 'redraw' was called on null.
I/flutter ( 7139): Receiver: null
I/flutter ( 7139): Tried calling: redraw

Sometimes, the Animation gets displayed but other times I see the Flutter error screen.

What's the issue here?

breitburg commented 3 years ago

Same issue

luigi-rosso commented 3 years ago

I suspect it's because _riveArtboard is null while the file is loading from the bundle. Check how the examples handle this condition: https://github.com/rive-app/rive-flutter/blob/607a8a3ee4ca83b452ae92d0ad141cd02139c079/example/lib/example_animation.dart#L55

mjohnsullivan commented 3 years ago

@NachiketaVadera @breitburg closing this for now; if @luigi-rosso 's suggestion has not helped resolve the problem, please reopen.