我正在为 child 创建一个应用程序。它具有菜单屏幕,从中可以通过单击相应的按钮进入不同的游戏。游戏完成后,开始播放星星动画。动画制作完成后,我希望我的应用返回到菜单屏幕。这是问题所在。动画结束后,我将收到以下错误消息: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,
    ),
  );
}
这是异常(exception)的日志:
════════ 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
══════════════════════════════════════════════════════════════
这是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!

最佳答案

好吧,很难确切地说出来,因为显然不是所有的代码都会显示出来。
但是我看不到将某些内容推送到Navigator的 View 堆栈中的位置。我看到您正在从导航器中弹出,但是如果您之前确实在其中推送了内容,则该代码不会显示在您的代码中。
同样对我来说,您似乎没有正确使用AnimatedSwitcher。该小部件假定其子项可以更改,并且当该子项更改时,它会应用动画,以便新旧子项之间的过渡看起来“不错”。在您的代码中,您只需要根据AnimatedSwitcher的值使用_gameFinished的两个不同实例。尝试考虑一下并更改您的实现,希望对您有所帮助。

关于flutter - getter 'userGestureInProgress'在null上被调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58528212/

10-10 08:54