使用 GoogleFonts textTheme 时 Flutter !_needsLayout 错误

Flutter !_needsLayout error when using GoogleFonts textTheme

我目前正在使用 Flutter 构建一个 Web 应用程序,它使用 GoogleFonts 包中的 textTheme。它在 MaterialApp 小部件中说明:

return MaterialApp(
  ...
  theme: ThemeData.light().copyWith(
    textTheme: GoogleFonts.rubikTextTheme(Theme.of(context).textTheme),
  ),
  ...
);

在启动或重新启动应用程序后的 1-2 秒内,如果鼠标光标悬停在通常的 Text(...) 小部件上,

中会发生错误
"flutter\packages\flutter\lib\src\painting\text_painter.dart"
 (line 881 in "getPositionForOffset" function in "assert(!_needsLayout);").

如果光标在启动后的几秒钟内没有位于包含文本的小部件上,则不会相应地发生错误。 如果您从 GoogleFonts 中删除对 textTheme 的使用,则不会发生此错误。错误的出现也不取决于所选的字体。

以下是错误本身的部分内容:

Error: Assertion failed:
!_needsLayout
is not true
at Object.throw_ [as throw] (http://localhost:53835/dart_sdk.js:5348:11)
at Object.assertFailed (http://localhost:53835/dart_sdk.js:5284:15)
at text_painter.TextPainter.new.getPositionForOffset (http://localhost:53835/packages/flutter/src/painting/placeholder_span.dart.lib.js:1432:49)
at paragraph.RenderParagraph.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/paragraph.dart.lib.js:643:45)
at paragraph.RenderParagraph.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:8111:44
at box.BoxHitTestResult.wrap.addWithPaintOffset (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:7850:19)
at flex$.RenderFlex.new.defaultHitTestChildren (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:8108:30)
at flex$.RenderFlex.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/flex.dart.lib.js:1206:19)
at flex$.RenderFlex.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at http://localhost:53835/packages/flutter/src/rendering/sliver.dart.lib.js:1413:24
at box.BoxHitTestResult.wrap.addWithOutOfBandPosition (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:7893:19)
at sliver_fill.RenderSliverFillRemaining.new.hitTestBoxChild (http://localhost:53835/packages/flutter/src/rendering/sliver.dart.lib.js:1411:21)
at sliver_fill.RenderSliverFillRemaining.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/sliver.dart.lib.js:1514:43)
at sliver_fill.RenderSliverFillRemaining.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/sliver.dart.lib.js:1002:28)
at http://localhost:53835/packages/flutter/src/rendering/sliver.dart.lib.js:2098:28
at box.BoxHitTestResult.wrap.addWithOutOfBandPosition (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:7893:19)
at viewport.RenderViewport.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/sliver.dart.lib.js:2096:30)
at viewport.RenderViewport.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderIgnorePointer.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderIgnorePointer.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderIgnorePointer.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:3479:59)
at proxy_box.RenderSemanticsAnnotations.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsAnnotations.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderPointerListener.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderPointerListener.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:501:36)
at proxy_box.RenderSemanticsGestureHandler.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsGestureHandler.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:501:36)
at proxy_box.RenderPointerListener.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderPointerListener.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:501:36)
at scrollable$._RenderScrollSemantics.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at scrollable$._RenderScrollSemantics.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderRepaintBoundary.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderRepaintBoundary.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at custom_paint.RenderCustomPaint.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at custom_paint.RenderCustomPaint.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/custom_paint.dart.lib.js:321:20)
at custom_paint.RenderCustomPaint.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderMouseRegion.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderMouseRegion.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderMouseRegion.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:3248:30)
at proxy_box.RenderPointerListener.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderPointerListener.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:501:36)
at proxy_box.RenderSemanticsGestureHandler.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsGestureHandler.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:501:36)
at proxy_box.RenderRepaintBoundary.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderRepaintBoundary.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at http://localhost:53835/packages/flutter/src/rendering/shifted_box.dart.lib.js:159:47
at box.BoxHitTestResult.wrap.addWithPaintOffset (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:7850:19)
at shifted_box.RenderPadding.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/shifted_box.dart.lib.js:156:23)
at shifted_box.RenderPadding.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:8111:44
at box.BoxHitTestResult.wrap.addWithPaintOffset (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:7850:19)
at custom_layout.RenderCustomMultiChildLayoutBox.new.defaultHitTestChildren (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:8108:30)
at custom_layout.RenderCustomMultiChildLayoutBox.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/custom_layout.dart.lib.js:295:19)
at custom_layout.RenderCustomMultiChildLayoutBox.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at material._RenderInkFeatures.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at material._RenderInkFeatures.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderPhysicalModel.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderPhysicalModel.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderPhysicalModel.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:2174:20)
at proxy_box.RenderSemanticsAnnotations.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsAnnotations.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderRepaintBoundary.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderRepaintBoundary.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderIgnorePointer.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderIgnorePointer.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderIgnorePointer.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:3479:59)
at proxy_box.RenderAnimatedOpacity.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderAnimatedOpacity.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderFractionalTranslation.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:3048:24
at box.BoxHitTestResult.wrap.addWithPaintOffset (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:7850:19)
at proxy_box.RenderFractionalTranslation.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:3046:21)
at proxy_box.RenderFractionalTranslation.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:3039:19)
at proxy_box.RenderRepaintBoundary.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderRepaintBoundary.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderSemanticsAnnotations.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsAnnotations.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderOffstage.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderOffstage.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderOffstage.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:3585:59)
at proxy_box.RenderSemanticsAnnotations.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsAnnotations.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at http://localhost:53835/packages/flutter/src/widgets/widget_span.dart.lib.js:32394:42
at box.BoxHitTestResult.wrap.addWithPaintOffset (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:7850:19)
at overlay$._RenderTheatre.new.hitTestChildren (http://localhost:53835/packages/flutter/src/widgets/widget_span.dart.lib.js:32391:28)
at overlay$._RenderTheatre.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderSemanticsAnnotations.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsAnnotations.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderAbsorbPointer.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderAbsorbPointer.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderAbsorbPointer.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:3661:79)
at proxy_box.RenderPointerListener.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderPointerListener.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:501:36)
at proxy_box.RenderSemanticsAnnotations.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsAnnotations.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderSemanticsAnnotations.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsAnnotations.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at proxy_box.RenderSemanticsAnnotations.new.hitTestChildren (http://localhost:53835/packages/flutter/src/rendering/proxy_box.dart.lib.js:404:56)
at proxy_box.RenderSemanticsAnnotations.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4356:28)
at view.RenderView.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:4648:58)
at binding.WidgetsFlutterBinding.new.hitTest (http://localhost:53835/packages/flutter/src/rendering/layer.dart.lib.js:5477:23)
at binding.WidgetsFlutterBinding.new.[_handlePointerEventImmediately] (http://localhost:53835/packages/flutter/src/gestures/binding.dart.lib.js:324:14)
at binding.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:53835/packages/flutter/src/gestures/binding.dart.lib.js:316:43)
at binding.WidgetsFlutterBinding.new.[_flushPointerEventQueue] (http://localhost:53835/packages/flutter/src/gestures/binding.dart.lib.js:305:14)
at binding.WidgetsFlutterBinding.new.[_handlePointerDataPacket] (http://localhost:53835/packages/flutter/src/gestures/binding.dart.lib.js:295:65)
at Object.invoke1 (http://localhost:53835/dart_sdk.js:186355:7)
at _engine.EnginePlatformDispatcher.__.invokeOnPointerDataPacket (http://localhost:53835/dart_sdk.js:166281:15)
at _engine.PointerBinding.__.[_onPointerData] (http://localhost:53835/dart_sdk.js:166941:49)
at http://localhost:53835/dart_sdk.js:167387:26
at http://localhost:53835/dart_sdk.js:167352:16
at http://localhost:53835/dart_sdk.js:167045:11

如果您遇到这个问题,我认为最简单的解决方案就是从 fonts.google.com 手动下载字体,将其添加到 Pubspec.yaml 并在 ThemeData 中设置。