flutter - 在 null 上调用了 getter 'userGestureInProgress'

标签 flutter dart

我正在为 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 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正确。这个小部件假定它的 child 可以改变,当 child 改变时,它会应用一个动画,所以新旧 child 之间的过渡看起来“很好”。在您的代码中,您只需使用 AnimatedSwitcher 的两个不同实例。取决于 _gameFinished值(value)。尝试考虑一下并更改您的实现,我希望它会有所帮助。

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

相关文章:

flutter - Flutter WebView 是否支持 WebGL?

Flutter - 如何仅在读取共享首选项后才绘制小部件?

android - Flutter Dart 在构建期间使用过多内存

import - Dart ,少和@import:FileError: 'file.less'未找到

git - Dart - 如何检查 Git 是否安装正确?

testing - Flutter Mockito 验证传递给小部件的回调被调用

flutter - type固定时根据Flutter中选择的item改变整个底部导航栏的背景颜色

Flutter iOS 构建错误 : framework not found Flutter

flutter - 如何在 flutter 中使用 dart.core.sink

flutter - Flutter Provider,使用子小部件来更新列表