Scaffold.geometryOf() 只能在绘制阶段访问

问题描述 投票:0回答:2

在windows上调试时抛出异常,stacktrace:

ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Scaffold.geometryOf() must only be accessed during the paint phase.
The ScaffoldGeometry is only available during the paint phase, because its value is computed during the animation and layout phases prior to painting.
#0      _ScaffoldGeometryNotifier.value.<anonymous closure> (package:flutter/src/material/scaffold.dart:835:9)
#1      _ScaffoldGeometryNotifier.value (package:flutter/src/material/scaffold.dart:842:6)
#2      _BottomAppBarClipper.getClip (package:flutter/src/material/bottom_app_bar.dart:238:35)

堆栈跟踪暗示从 BottomAppBar 获取几何值,这就是我在小部件中的内容。当我在屏幕上移动鼠标时,此错误之后会出现大量 mouse_tracker 的错误消息:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:flutter/src/rendering/mouse_tracker.dart': Failed assertion: line 195 pos 12: '!_debugDuringDeviceUpdate': is not true.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
#2      MouseTracker._deviceUpdatePhase (package:flutter/src/rendering/mouse_tracker.dart:195:12)

此后屏幕上没有任何内容可点击。

仅在按下 FloatingActionButton 后才会发生该错误,而按下页面上的后退按钮则不会发生该错误。 FAB 的 onPressed 是:

  void onOkPressed() {
    Navigator.of(context).pop();
  }

如何解决这个错误?

flutter floating-action-button flutter-windows
2个回答
0
投票

FAB 包含在底部应用栏中,尝试将 FAB 位置设置为浮动

来自

Scaffold(
    appBar: const AppBar(),
    bottomNavigationBar: const BottomAppBar(),
    floatingActionButton: FloatingActionButton(
      onPressed: onOkPressed,
      child: const Icon(Icons.check, size: 32),
    ),
    floatingActionButtonLocation: FloatingActionButtonLocation.endContained,
....

Scaffold(
    appBar: const AppBar(),
    bottomNavigationBar: const BottomAppBar(),
    floatingActionButton: FloatingActionButton(
      onPressed: onOkPressed,
      child: const Icon(Icons.check, size: 32),
    ),
    floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
....

0
投票

我遇到了完全相同的问题。在导航器中推送第二个页面时将maintainState设置为true,似乎可以解决问题。

Navigator.push(
       context,
       MaterialPageRoute(
          maintainState: true, // <------ change this
          builder: (context) => SecondPageWithFAB()
          )
       );

然后在SecondPageWithFAB中你可以像以前一样调用Navigator.pop:

void onOkPressed() {
    Navigator.of(context).pop();
  }
© www.soinside.com 2019 - 2024. All rights reserved.