[障害対応]ParentDataWidget Expanded wants to apply ParentData of type FlexParentData to aRenderObject, which has been set up to accept ParentData of incompatible type BoxParentData.

1.発生事象:

デバッグモード実行時( flutter run -d windows )後、アプリの検索用アイコンをクリックした際に、ターミナルにエラーが発生。

外観は仕様通りに検索用TextFieldが表示されFocus監視・動作も正常に見えていたのですが、リリースビルド後に端末試験で(検索用TextFieldがFocusを失い制御不能、TextFieldもブランクで操作不能になるという事象が)発生し、再度開発環境でログ確認後に本件は発覚しました。

2.エラー内容:

Dart
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a
RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData.
Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically,
Expanded widgets are placed directly inside Flex widgets.
The offending Expanded is currently placed inside a _AppBarTitleBox widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
  MouseRegionTextFieldExpanded_AppBarTitleBoxSemanticsDefaultTextStyleMediaQuery
BuilderLayoutId-[<_ToolbarSlot.middle>] ← CustomMultiChildLayout ← ⋯

When the exception was thrown, this was the stack:
#0      RenderObjectElement._updateParentData.<anonymous closure>
(package:flutter/src/widgets/framework.dart:6545:11)
#1      RenderObjectElement._updateParentData (package:flutter/src/widgets/framework.dart:6562:6)
#2      RenderObjectElement.attachRenderObject (package:flutter/src/widgets/framework.dart:6605:7)
#3      RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6467:5)
#4      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6768:11)
...     Normal element mounting (15 frames)
(続く)
エラー内容(全部):
=== EXCEPTION CAUGHT BY WIDGETS LIBRARY =================================================================
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.
The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a
RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData.
Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically,
Expanded widgets are placed directly inside Flex widgets.
The offending Expanded is currently placed inside a _AppBarTitleBox widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
MouseRegion ← TextField ← Expanded ← _AppBarTitleBox ← Semantics ← DefaultTextStyle ← MediaQuery ←
Builder ← LayoutId-[<_ToolbarSlot.middle>] ← CustomMultiChildLayout ← ?

When the exception was thrown, this was the stack:
#0 RenderObjectElement._updateParentData.<anonymous closure>
(package:flutter/src/widgets/framework.dart:6545:11)
#1 RenderObjectElement._updateParentData (package:flutter/src/widgets/framework.dart:6562:6)
#2 RenderObjectElement.attachRenderObject (package:flutter/src/widgets/framework.dart:6605:7)
#3 RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6467:5)
#4 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6768:11)
... Normal element mounting (15 frames)
#19 Element.inflateWidget (package:flutter/src/widgets/framework.dart:4340:16)
#20 Element.updateChild (package:flutter/src/widgets/framework.dart:3843:20)
#21 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#22 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#23 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#24 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#26 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#27 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#28 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#30 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#31 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#32 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#33 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#34 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#35 StatelessElement.update (package:flutter/src/widgets/framework.dart:5563:5)
#36 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#38 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#39 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#40 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#41 Element.updateChildren (package:flutter/src/widgets/framework.dart:3976:32)
#42 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6929:17)
#43 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#45 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#46 StatelessElement.update (package:flutter/src/widgets/framework.dart:5563:5)
#47 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#48 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#49 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#50 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#51 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#52 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#53 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#54 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#55 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#56 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#57 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#58 StatelessElement.update (package:flutter/src/widgets/framework.dart:5563:5)
#59 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#60 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#61 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#62 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#63 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#64 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#65 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#66 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#67 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#68 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#69 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#70 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#71 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#72 StatelessElement.update (package:flutter/src/widgets/framework.dart:5563:5)
#73 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#74 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#75 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#76 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#77 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#78 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#79 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#80 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#81 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#82 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#83 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#84 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#85 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#86 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#87 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#88 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#89 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#90 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#91 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#92 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#93 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#94 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#95 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#96 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#97 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#98 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#99 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#100 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#101 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#102 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#103 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#104 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#105 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#106 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#107 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#108 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#109 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#110 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#111 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#112 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#113 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#114 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#115 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#116 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#117 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#118 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#119 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#120 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#121 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#122 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#123 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#124 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#125 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#126 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#127 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#128 Element.updateChildren (package:flutter/src/widgets/framework.dart:3976:32)
#129 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6929:17)
#130 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#131 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#132 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#133 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#134 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#135 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#136 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#137 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#138 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#139 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#140 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#141 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#142 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#143 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#144 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#145 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#146 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#147 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#148 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#149 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#150 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#151 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#152 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#153 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#154 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#155 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#156 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#157 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#158 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#159 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#160 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6776:14)
#161 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#162 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#163 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#164 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#165 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#166 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#167 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#168 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#169 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#170 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#171 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#172 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#173 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#174 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#175 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#176 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#177 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#178 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#179 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#180 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#181 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#182 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#183 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#184 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#185 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#186 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#187 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#188 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#189 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#190 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#191 ProxyElement.update (package:flutter/src/widgets/framework.dart:5816:5)
#192 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#193 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#194 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#195 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#196 StatefulElement.update (package:flutter/src/widgets/framework.dart:5673:5)
#197 Element.updateChild (package:flutter/src/widgets/framework.dart:3827:15)
#198 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5512:16)
#199 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5650:11)
#200 Element.rebuild (package:flutter/src/widgets/framework.dart:5203:7)
#201 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2905:19)
#202 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1136:21)
#203 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:443:5)
#204 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1392:15)
#205 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1313:9)
#206 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1171:5)
#207 _invoke (dart:ui/hooks.dart:312:13)
#208 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:419:5)
#209 _drawFrame (dart:ui/hooks.dart:283:31)
==========================================================================================================

3.原因:

上記ログ(4行目~5行目)に、ParentDataWidget Expanded (Expanded ウィジェットが問題の発端)、
wants to apply ParentData of type FlexParentData to a
RenderObject, which has been set up to accept ParentData of incompatible type BoxParentData.

((Expanded ウィジェットは、FlexParentData を提供しようとしているのに)RenderObject 側は BoxParentData を欲しがっている
とあります。
つまり、Expanded の使い方がおかしい! とあります。

さて、どこのExpanded か?というと、上記で発生したタイミングは明確(検索アイコンクリック時)なので、修正箇所は、以降の通りになりました。

4.修正内容:

修正箇所は、以下(修正前)コード(5行目~6行目)の Expanded の部分です。

修正前
  // 2.5 アプリバー(AppBar/title)構築ウィジェットの生成(_buildAppBarTitle())
  Widget _buildAppBarTitle() {
    if (isSearching) {
    // 2.5.1) 検索モード時(isSearching): 検索キーワード入力欄(TextField)を表示
      return Expanded(
        child: TextField(
          controller: searchController, // 検索キーワード用コントローラ
          focusNode: _focusNode, // フォーカスノードを追加
          decoration: const InputDecoration(
            hintText: '検索キーワードを入力して下さい', // プレースホルダー
            hintStyle: TextStyle(color: Colors.blue), // プレースホルダーのテキストの色
            enabledBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.blue), // アンダーバーの色:青色
            ),
            focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Color.fromARGB(255, 33, 54, 243)), // フォーカス時のアンダーバーの色:青色
            ),
          ),
          style: const TextStyle(color: Colors.blue, fontSize: 18),
        ),
      );
    }else{
    //   2.5.2) 非検索モード時: タイトル(カテゴリ表示欄(DropdownButton))を表示
      return DropdownButton<String>(

上記の Expanded を、Row に Refact する(以下の5行目~8行目)ことで、問題は解消しました!

修正後
  // 2.5 アプリバー(AppBar/title)構築ウィジェットの生成(_buildAppBarTitle())
  Widget _buildAppBarTitle() {
    if (isSearching) {
    // 2.5.1) 検索モード時(isSearching): 検索キーワード入力欄(TextField)を表示
      return Row(
        children: [
          Expanded(
            child: TextField(
              controller: searchController, // 検索キーワード用コントローラ
              focusNode: _focusNode, // フォーカスノードを追加
              decoration: const InputDecoration(
                hintText: '検索キーワードを入力して下さい', // プレースホルダー
                hintStyle: TextStyle(color: Colors.blue), // プレースホルダーのテキストの色
                enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.blue), // アンダーバーの色:青色
                ),
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(color: Color.fromARGB(255, 33, 54, 243)), // フォーカス時のアンダーバーの色:青色
                ),
              ),
              style: const TextStyle(color: Colors.blue, fontSize: 18),
            ),
          ),
        ],
      );
    }else{
    //   2.5.2) 非検索モード時: タイトル(カテゴリ表示欄(DropdownButton))を表示
      return DropdownButton<String>(

上記対応後、リリースビルド時の問題も解消しました。

コメントを残す