0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Flutter】Expandedしない方がいいケースもある

Last updated at Posted at 2025-10-15

エラー

Terminal.
══╡ 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 Padding widget.
The ownership chain for the RenderObject that received the incompatible parent data was:
  RichText ← Text ← Expanded ← Padding ← RepaintBoundary ← IndexedSemantics ← _SelectionKeepAlive ←
NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← ⋯

対象のコード

return Padding(
    padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
    child: Expanded(
      child: 
      Text("小説を選択する",
        style: TextStyle(
          fontSize: 24,
          fontWeight: FontWeight.w700
        ),
      )
    ),
);

解決策

  • ExpandedモディファイアをPaddingの中で使用しない。

理由

  • ExpandedRow, Columnn, Flexの子孫でなければならない。
    • Row, Columnn, Flexの主軸に沿って拡張するという仕様があるから。
    • Paddingはそれに該当しないので使わないでくださいということ。

An Expanded widget must be a descendant of a Row, Column, or Flex, and the path from the Expanded widget to its enclosing Row, Column, or Flex must contain only StatelessWidgets or StatefulWidgets (not other kinds of widgets, like RenderObjectWidgets).

追記

  • ListViewの中でExpandedを用いてはいけません。その理由はこちら。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?