rive-app / rive-flutter

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

Flutter web: invalid member on null: advance #34

Closed apomalyn closed 3 years ago

apomalyn commented 3 years ago

Hi!

I'm trying to use an animation realized with Rive2, but when the render start the following error is thrown:

The following JSNoSuchMethodError was thrown during paint():
NoSuchMethodError: invalid member on null: 'advance'

The relevant error-causing widget was: 
  Rive file:///Users/CHRX001/Documents/dev/portfolio/lib/widgets/scroll_down_animated.dart:40:54
When the exception was thrown, this was the stack: 
packages/rive/src/rive.dart 71:52                                        advance
packages/rive/src/rive_render_box.dart 111:9                             paint
packages/flutter/src/rendering/object.dart 2311:7                        [_paintWithContext]
packages/flutter/src/rendering/object.dart 189:12                        paintChild
packages/flutter/src/rendering/proxy_box.dart 129:14                     paint
...
The following RenderObject was being processed when the exception was fired: RiveRenderObject#a17bf
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(w=42.0, h=42.0)
...  size: Size(42.0, 42.0)
RenderObject: RiveRenderObject#a17bf
  parentData: <none> (can use size)
  constraints: BoxConstraints(w=42.0, h=42.0)
  size: Size(42.0, 42.0)

And there is the class I use:

class ScrollDownAnimated extends StatefulWidget {
  _ScrollDownAnimatedState createState() => _ScrollDownAnimatedState();
}

class _ScrollDownAnimatedState extends State<ScrollDownAnimated> {
  Artboard _artboard;
  RiveAnimationController _controller;

  @override
  void initState() {
    super.initState();

    rootBundle.load('animations/scroll_down.riv').then((data) async {
      final file = RiveFile();

      if (file.import(data)) {
        final artboard = file.mainArtboard;

        artboard.addController(_controller = SimpleAnimation('default'));
        setState(() => _artboard = artboard);
      }
    });
  }

  @override
  Widget build(BuildContext context) => Center(
        child: Column(
          children: [
            Text('Scroll',
                style: AppTheme.theme.textTheme.bodyText1
                    .copyWith(color: AppTheme.white)),
            SizedBox(height: AppTheme.instance.smallVerticalSpacing / 3),
            SizedBox(width: 42, height: 42, child: Rive(artboard: _artboard))
          ],
        ),
      );
}

If anyone have an idea ? ^^'

Floul commented 3 years ago

try to change this: SizedBox(width: 42, height: 42, child: Rive(artboard: _artboard))

to this:

SizedBox(
        width: 42, 
        height: 42, 
        child: _riveArtBoard == null
            ? const SizedBox()
            : Rive(artboard: _riveArtBoard),
)

in other words it seems like _artboard is not initialized yet when widget is building

apomalyn commented 3 years ago

That was exactly that! Thank you!