【问题标题】:setState() or markNeedsBuild() called during build when playing rive animationsetState() 或 markNeedsBuild() 在播放动画时在构建期间调用
【发布时间】:2021-07-28 15:02:37
【问题描述】:

我在尝试调用此函数时遇到错误 setState() 或 markNeedsBuild() 在构建期间调用:

void togglePlay() {
    if (_controller == null) {
      return;
    }
    setState(() => _controller.isActive = !_controller.isActive);
  }

当我将它与按钮一起用作 onTap 功能时,不会发生错误。但我只需要使用其他函数调用。

完整代码如下:

void togglePlay() {
    if (_controller == null) {
      return;
    }
    setState(() => _controller.isActive = !_controller.isActive);
  }

  bool get isPlaying => _controller?.isActive ?? false;
  Artboard _riveArtboard;
  RiveAnimationController _controller;
  // rive

  @override
  void initState() {
    print('guess position');
    // rive
    try {
      rootBundle.load('assets/match.riv').then(
        (data) async {
          final file = RiveFile.import(data);
          final artboard = file.mainArtboard;
          artboard.addController(_controller = SimpleAnimation('Animation 1'));
          _controller.isActive = false;
          setState(() => _riveArtboard = artboard);
        },
      );
    } catch (e) {
      print(e.toString());
    }
    // rive
    super.initState();
  }

  List top = [];
  List left = [];
  List rotation = [];

  next() {
    List correctT = [];
    List correct = [];
    for (int n = 0; n < widget.rotation.length; n++) {
      for (int m = 0; m < widget.rotation.length; m++) {
        if (rotation[m] == widget.rotation[n] &&
            top[m] < widget.top[n] + 45 &&
            top[m] > widget.top[n] - 45 &&
            left[m] < widget.left[n] + 45 &&
            left[m] > widget.left[n] - 45) {
          log('widget match $m qgueals  match $n');
          if (!correctT.contains(n) && !correct.contains(m)) {
            correctT.add(n);
            correct.add(m);
          }
        }
      }
    }
    if (correctT.length == widget.rotation.length) {
      log('nice');
      togglePlay();  // FUNCTION CAUSING ERROR
    }
  }

  nav() {
    SchedulerBinding.instance.addPostFrameCallback((_) {
      Navigator.push(
          context, new MaterialPageRoute(builder: (context) => RandomPos(2)));
    });
  }

  bool created = false;
  createList() {
    if (!created) {
      for (int i = 0; i < widget.top.length; i++) {
        top.add(h - 250);
        left.add(50);
        rotation.add(0);
      }
      setState(() {
        created = true;
      });
    }
  }

  match(int i) {
    return Positioned(
        top: top[i].toDouble(),
        left: left[i].toDouble(),
        child: RotationTransition(
          turns: new AlwaysStoppedAnimation(rotation[i] / 360),
          child: GestureDetector(
            onTap: () {
              rotation[i] = rotation[i] + 45;
              setState(() {});
              next();
            },
            onDoubleTap: () {
              rotation[i] = rotation[i] - 45;
              setState(() {});
              next();
            },
            child: Draggable(
                data: 10,
                onDragUpdate: (details) {
                  top[i] = top[i] + details.delta.dy;
                  left[i] = left[i] + details.delta.dx;
                  setState(() {});
                },
                onDragEnd: next(),
                child: Container(
                  height: 250,
                  width: 250,
                  child: _riveArtboard == null
                      ? const SizedBox()
                      : Rive(artboard: _riveArtboard),
                ),
                feedback: Container()),
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
          future: createList(),
          builder: (context, snapshot) {
            return Container(
              decoration: BoxDecoration(
                  image: DecorationImage(
                      fit: BoxFit.fill,
                      image: AssetImage('wood${widget.wood}.jpg'))),
              child: Stack(
                children: [for (int i = 0; i < rotation.length; i++) match(i)],
              ),
            );
          }),
      floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.white,
          child: Text(
            'next',
            style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
          ),
          onPressed: () => nav()),
    );
  }
}

如果你能看一下并帮助我解决问题,谢谢

一些典型的“在重建期间调用设置状态”的解决方案不起作用或导致其他错误

【问题讨论】:

    标签: flutter dart rive


    【解决方案1】:

    当我们在构建阶段调用 setState 时会出现此错误。

    使用在构建渲染完成后运行的addPostFrameCallback

    void togglePlay() {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (_controller == null) {
          return;
        }
        setState(() => _controller.isActive = !_controller.isActive);
      });
    }
    

    或

      Timer.run(() {
        if (_controller == null) {
          return;
        }
        setState(() => _controller.isActive = !_controller.isActive);
      });
    

    【讨论】:

    • 谢谢,但它对我不起作用 - togglePlay() 没有启动,整个 next() 一次又一次地循环
    • @helloitsme3:添加了另一个应该可行的解决方案
    • 它给出的结果与以前的解决方案一样。也许我在接下来的 function() 中调用 TogglePlay() 时犯了错误?
    • 这个函数,你确定 next() togglePlay() 被调用了吗?如果可以,您能否准确分享从您的代码调用它的位置?
    • togglePlay() 是 next() 函数的最后一节,即使上面的行有效,它也不会被调用(例如在控制台'nice'中写入)
    猜你喜欢
    • 2018-05-15
    • 2020-12-12
    • 2020-12-28
    • 2021-01-06
    • 2021-06-06
    • 2021-05-31
    • 2021-08-24
    相关资源
    最近更新 更多