15
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Incorrect use of ParentDataWidget(ParentDataWidget の不適切な使用エラー) 

Last updated at Posted at 2022-12-30

最近Flutterの開発で初心者の自分はこのエラーと出会って、解決策をこの記事にまとめました。

エラーメッセージ


══╡ 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 ← Container ← _BodyBuilder ← MediaQuery ←
LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← AnimatedBuilder ← ⋯

原因

このエラーは、Flexible(), Expanded(), Positioned(), TableCell() ウィジェットなどの子ウィジェットに、該当する親ウィジェットがない場合に発生します。 例えば


Container(
  Expanded(
    child: Text("Hello FlutterCampus")
  )        
) //Error: Incorrect use of ParentDataWidget

解決策

Widget Parent Widget
Expanded() Row(), Column(), Flex()
Flexible() Row(), Column(), Flex()
Positioned() Stack()
TableCell() Table()

Example Code


Row( 
  children:[
    Expanded(
      child: Text("Hello FlutterCampus")
    )
  ]
)

OR:


Stack( 
  children:[
    Positioned(
      child: Text("Hello FlutterCampus")
    )
  ]
)

参考記事

15
5
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
15
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?