Date Archives November 2022

SOLVED: Incorrect use of ParentDataWidget Error in Flutter

In this post, we are going to show you the cause and how to solve the “Incorrect use of ParentDataWidget” error in Flutter. This error occurs when the Child widget has not matched the parent widget. 

 Error Message:

══╡ 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 ← ⋯

In our case, this error happens due to the Expanded() widget.

 Cause of error:

This error happens when child widgets like Flexible()Expanded()Positioned() and TableCell() widget has no matching parent widget.  For example:

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

Here, Expanded() widget must have Row()Column() or Flex() widget as a Parent widget. 

 Solution of the Error:

To solve this error, your child widget must have the expected parent widget. Some of the widgets and their parent widgets are:

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

For Example:

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

OR:

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

Here, Row() is the parent widget of Expanded(), and Stack() is the parent of Positioned() widget. Similarly, see the above chart list, and use the parent according to the child widget.

In this way, you can solve the “Incorrect use of ParentDataWidget” Error in the Flutter App

Solved: Flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request.

HandshakeException: Handshake error in client (OS Error: E/flutter ( 6264):  CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:363))

In order to enable this option globally in your project, here is what you need to do:

  • In your main.dart file, add or import the following class:
 class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext? context){
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
  }
}
  • In your main function, add the following line after function definition:
HttpOverrides.global = MyHttpOverrides();

This should be used while in development mode, do NOT do this when you want to release to production, the aim of this answer is to make the development a bit easier for you, for production, you need to fix your certificate issue and use it properly, look at the other answers for this as it might be helpful for your case.