Search code examples
flutterfloating-action-buttonflutter-windows

Scaffold.geometryOf() must only be accessed during the paint phase


Exception is thrown when debugging on 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)

The stacktrace is hinting at getting geometry values from a BottomAppBar which is what I have in my widget. This error is followed by a lot of error messages for mouse_tracker when I move the mouse on the screen:

[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)

Nothing on the screen is clickable after this.

The error occurs only after the FloatingActionButton is pressed, and not if the back button on the page is pressed. The onPressed for the FAB is:

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

How can I solve this error?


Solution

  • The FAB is contained in the bottom app bar, try setting the FAB location to float

    from

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

    to

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