flutter – Incorrect use of parent data widget. expanded widgets must be placed inside flex widgets – Code Utility

[

I am getting the following error:

i.e.., Another exception was thrown: Incorrect use of ParentDataWidget.
showing error on the mobile screen.

 @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: widget.title,
      theme: ThemeData.light().copyWith(
        platform: _platform ?? Theme.of(context).platform,
      ),
      home: DefaultTabController(
        length: categoryNames.length,
        child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
                 ),
        body: SafeArea(
            child: Column(
              children: <Widget>[
                Chewie(
                  controller: _chewieController,
                ),
                TabBar(
                  labelColor:Colors.black,
                  tabs: categoryNames,
                ),
                Expanded(
                  child: TabBarView(
                    children: [
                      ImageList()
                    ],
                  ),
                )
                /*TabBarView(
                  children: [
                    Icon(Icons.directions_car),
                    Icon(Icons.directions_transit),
                    Icon(Icons.directions_bike),
                  ],
                )*/
              ],
            )
        ),
      ),
      ),
    );
  }

It’s my code, please check and let me know the issue.

,

Expanded cannot be used inside a Stack.

You should use Expanded only within a Column, Row or Flex

,

What does the error meaning

This error is about missing an expected parent widget.

In this case just removing Expanded or wrapping it in a Column should fix the problem.

While Flutter’s widgets are generally flexible in how they can be composed together in a UI, a small subset of those widgets expect specific parent widgets. When this expectation can’t be satisfied in your widget tree, you’re likely to see this error.

Here is an incomplete list of widgets that expect specific parent widgets within the Flutter framework. Feel free to submit a PR (using the doc icon in the top right corner of the page) to expand this list.

|              Widget                       |    Expected parent widget(s)    |
| ---------------------------------------   | ------------------------------- |
|         Flexible                          |         Row, Column, or Flex    |
|         Expanded (a specialized Flexible) |         Row, Column, or Flex    |
|         Positioned                        |         Stack                   |
|         TableCell                         |         Table                   |

To fix it

The fix should be obvious once you know which parent widget is missing.

An Expanded widget must be a descendant of a Row, Column, or Flex

The fix for this above issue, is to move the expanded widget inside a flex widget. Example below.

Row(
    children: [
      Expanded(
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.only(bottom: 8.0),
              child: Text(
                "Some Lage Somthing",
                style: TextStyle(fontWeight: FontWeight.bold),
              ),
            ),
            Text("Someplace, Country")
          ],
        ),
      ),
      Icon(Icons.star)
    ],
  ),
);

Happy Fluttering!

,

First Rule: use Expanded only within a column, row or flex.

Second Rule: Parent column that have expanded child column must be wrapped with expanded as well

,

there are may things to get this problem

  1. Under ListView don’t use Spacer Widget
  2. don’t use Positioned under Row or Column
  3. Expanded can only use it must be a descendant of Column Row Flex

,

there’s no expanded in my code so i found that using Spacer() too can cause this error if it’s inside a listview

,

In my case, I was using Positioned inside a column. Removing it solved the problem!

,

I removed a Positioned widget that was inside a Container inside a Stack and the problem “Incorrect use of ParentDataWidget” went away.

,

when I skip the video, the chewie controller shows these errors when I fix in chewie package error gone.

please fix in material_controls.dart here

[remove expanded widget in Stack widget]

return MouseRegion(
  onHover: (_) {
    _cancelAndRestartTimer();
  },
  child: GestureDetector(
    onTap: () => _cancelAndRestartTimer(),
    child: AbsorbPointer(
      absorbing: notifier.hideStuff,
      child: Stack(
        children: [
          if (_latestValue.isBuffering)
            const Expanded(
              child: Center(
                child: CircularProgressIndicator(),
              )
            )
          else
            _buildHitArea(),
          _buildActionBar(),
          Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              if (_subtitleOn)
                Transform.translate(
                  offset: Offset(
                      0.0, notifier.hideStuff ? barHeight * 0.8 : 0.0),
                  child:
                      _buildSubtitles(context, chewieController.subtitle!),
                ),
              _buildBottomBar(context),
            ],
          ),
        ],
      ),
    ),
  ),
);

}

,

This Solution works only if you are using Position as a parent of row or column

In my case, I was using Positioned inside a column. Removing it solved the problem!

Else

Always remember to use Expanded in Row, Column or Flex

,

In my case, I was using Expanded inside a Padding widget. Padding was inside a column but since Expanded was wrapped by a Padding it gave me the error. Simply removing Expanded solved my issue.

,

In my case it was because I was enclosing an Expanded inside a SizedBox, I just enclosed the Expanded inside a Row and that’s it.

]