setState()或markNeedsBuild()在构建时播放动画时调用



我得到错误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()),
);
}
}

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

重建过程中调用的典型"设置状态"的几个解决方案不工作或导致其他错误

当我们在构建阶段调用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);
});

最新更新