我正在为儿童创建一个应用程序。它有菜单屏幕,您可以通过单击适当的按钮转到不同的游戏。游戏完成后,开始播放星星动画。动画完成后,我希望我的应用程序返回到菜单屏幕。这是问题所在。动画完成后,我收到此错误:The getter 'userGestureInProgress' was called on null.也没有堆栈跟踪,因此我无法确定此错误发生的确切位置。
这是我的代码:
class OddOneOutPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return OddOneOutPageState();
}
}
class OddOneOutPageState extends State<OddOneOutPage>
with SingleTickerProviderStateMixin {
Widget _pageContent;
ValueNotifier<bool>_animationFinished;
Particles particles;
bool _gameFinished = false;
@override
void initState() {
super.initState();
_animationFinished = ValueNotifier(false);
_animationFinished.addListener(() {
if(_animationFinished.value) {
Navigator.pop(context); // this line causes a problem
}
});
particles = Particles(30, _animationFinished);
}
@override
Widget build(BuildContext context) {
Widget body;
// some code to check if game is finished
if (_gameFinished) {
body = Stack(
children: <Widget>[
AnimatedSwitcher(
duration: Duration(milliseconds: 300),
child: _pageContent,
),
Positioned.fill(child: particles)
],
);
} else {
body = AnimatedSwitcher(
duration: Duration(milliseconds: 300), child: _pageContent);
}
return Scaffold(
appBar: AppBar(
title: Text(MyLocalizations.of(context, StringKeys.oddOneOut)),
),
body: body,
),
);
}
Run Code Online (Sandbox Code Playgroud)
以下是异常日志:
???????? Exception caught by animation library ???????????????????????
The following assertion was thrown while notifying status listeners for AnimationController:
Build scheduled during frame.
While the widget tree was being built, laid out, and painted, a new frame was scheduled to rebuild the widget tree. This might be because setState() was called from a layout or paint callback. If a change is needed to the widget tree, it should be applied as the tree is being built. Scheduling a change for the subsequent frame instead results in an interface that lags behind by one frame. If this was done to make your build dependent on a size measured at layout time, consider using a LayoutBuilder, CustomSingleChildLayout, or CustomMultiChildLayout. If, on the other hand, the one frame delay is the desired effect, for example because this is an animation, consider scheduling the frame in a post-frame callback using SchedulerBinding.addPostFrameCallback or using an AnimationController to trigger the animation.
When the exception was thrown, this was the stack:
0 WidgetsBinding._handleBuildScheduled.<anonymous closure> (package:flutter/src/widgets/binding.dart:637:9)
1 WidgetsBinding._handleBuildScheduled (package:flutter/src/widgets/binding.dart:656:6)
2 BuildOwner.scheduleBuildFor (package:flutter/src/widgets/framework.dart:2232:7)
3 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3706:11)
4 State.setState (package:flutter/src/widgets/framework.dart:1161:14)
...
The AnimationController notifying status listeners was: AnimationController#677b4(? 1.000; for MaterialPageRoute<dynamic>(null))
????????????????????????????????????????????????????????????????????????????????????????????????????
???????? (2) Exception caught by widgets library ???????????????????????????????????????????????????
The getter 'userGestureInProgress' was called on null.
Receiver: null
Tried calling: userGestureInProgress
User-created ancestor of the error-causing widget was:
MaterialApp file:///C:/Users/olgam/Desktop/FlutterProjects/Projects/Kids%20Development/kids_development/lib/main.dart:10:12
??????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
这是 Flutter Doctor 结果:
[?] Flutter (Channel stable, v1.9.1+hotfix.4, on Microsoft Windows [Version 10.0.18362.418], locale en-US)
• Flutter version 1.9.1+hotfix.4 at C:\Users\olgam\source\flutter
• Framework revision cc949a8e8b (4 weeks ago), 2019-09-27 15:04:59 -0700
• Engine revision b863200c37
• Dart version 2.5.0
[?] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at C:\Users\olgam\AppData\Local\Android\sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
• All Android licenses accepted.
[?] Android Studio (version 3.5)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 40.2.2
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
[?] Connected device (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 7.0 (API 24) (emulator)
• No issues found!
Run Code Online (Sandbox Code Playgroud)
嗯,很难准确地说,因为显然没有显示所有代码。但我看不到您将某些内容推入Navigator视图堆栈的位置。我看到您正在从导航器中弹出,但如果您之前实际将某些内容推送到那里,则它不会显示在您的代码中。
对我来说,看来您没有AnimatedSwitcher正确使用。该小部件假定其子级可以更改,并且当子级更改时,它会应用动画,以便新旧子级之间的过渡看起来“不错”。AnimatedSwitcher在您的代码中,您只需根据值使用两个不同的实例_gameFinished。尝试考虑一下并改变你的实现,我希望这会有所帮助。
| 归档时间: |
|
| 查看次数: |
1170 次 |
| 最近记录: |