242

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.

My Car
  • 4,198
  • 5
  • 17
  • 50
Mounika
  • 3,348
  • 5
  • 16
  • 25
  • I've just run the code you've posted and I'm not getting the same error, would you be able to post the contents of `categoryNames` or the output of `ImageList()`? The issue is most likely an `Expanded` that's not the direct child of a `Column` or a `Row`, but without more info it's a bit hard to say where exactly it's coming from – greyaurora Feb 27 '19 at 12:33
  • categorynames is just a list of names and Imagelist is a class, issue is with expanded itself is showing but apart from this any alternate is there to acheive tabbarview with tabs – Mounika Feb 27 '19 at 12:37

14 Answers14

454

Expanded cannot be used inside a Stack.

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

iDecode
  • 22,623
  • 19
  • 99
  • 186
Bilal Awan
  • 4,730
  • 1
  • 8
  • 15
  • FWIW, I also got this issue when, after converting a `Column` to a `Stack`, I had a `Spacer` widget as one of the children of the `Stack`. I was scouring my code looking for "Expanded", but there was none. So basically, make sure you have neither an `Expanded` nor a `Spacer` as a child of your `Stack`. – David Chopin Aug 02 '23 at 15:51
92

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

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
secreal
  • 1,045
  • 7
  • 5
  • 1
    On my case, the issue is because the Expanded/Flexible is not a direct child(ren) in Column's children, but rather inside a GestureRecognizer which is the direct child(ren) of the Column. – Chen Li Yong Mar 11 '21 at 17:51
  • 28
    haven't heard a bigger nonsense yet. If expanded can only be a descendant of column, row or flexible how do you suggest we should wrap a column within expanded? – Romeo Mihalcea Aug 06 '21 at 14:14
66

there are many things to get this problem.

for example:

  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
Mohammad Rabiee Nasri
  • 1,309
  • 2
  • 7
  • 26
Akbar Masterpadi
  • 976
  • 8
  • 15
  • 1
    BINGOuuu! Specifically I had a `Spacer()` assigned to the `trailing` of a `ListTile` which is inside a `ListView`. It rendered OK but the error was logged and cause for concern. The big problem here was that the stacktrace does not mention my code at all, so it was difficult to track manually, looking at all the `Expanded()` instances. Example: `ListView( ListTile ( triling: Spacer() ) )` <- This is bad – eriel marimon Jun 02 '22 at 18:34
  • Thanks. Spacer() is the problems for me – Nas Shihab Aug 12 '23 at 07:42
9

My issue is because of Expand

          child: Expanded(
            child: SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Column(
                children: getChildren(cubit.listPerformer, state),
              ),

After delete Expand, it works fine.

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
6

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

oth man
  • 185
  • 4
  • 16
  • Did you find an alternative to use ```Spacer()``` inside the Scrollable item? I want to keep maximum space before the last column child and I want to eliminate Overflow on the Keyboard popup. – Hardik Aug 25 '21 at 10:27
5

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

M.AQIB
  • 359
  • 5
  • 11
5

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

mjukka
  • 61
  • 1
  • 2
  • 2
    It's good to try and help, but in this case there are already a lot of very detailed answers to this question and yours doesn't actually add anything. Before answering have a look at what other people have said, and think about whether you're adding anything new that's clearly relevant to the question. – Craig Graham Oct 06 '21 at 12:31
1

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),
            ],
          ),
        ],
      ),
    ),
  ),
);

}

1

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

Srikanth
  • 11
  • 5
1

Just remove the Expanded.

You should have to use Expanded whenever needed.

Check the below image for reference.

Removed expanded and it works perfect

Sanket Vanani
  • 244
  • 3
  • 7
1
 Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 25, vertical: 20),
                      child: Card(
                        child: SingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          child: Expanded(child: _buildDataTable(data, fields)),
                        ),
                      ),
                    )

give real life to my device emulator and during other emulator devices working fine

but this is not working on a real device like an Android phone, then follow the rule of

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

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

remove Expanded

Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 25, vertical: 20),
                      child: Card(
                        child: SingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          child: _buildDataTable(data, fields),
                        ),
                      ),
                    ),
0

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.

sithum dilanga
  • 427
  • 7
  • 14
0

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.

edalvb
  • 581
  • 6
  • 7
-1

You can use Expanded as child of scrolling parent like this:

Scaffold(
  body: CustomScrollView(
    physics: const AlwaysScrollableScrollPhysics(),
    slivers: [
      SliverFillRemaining(
        fillOverscroll: true,
        child: Column(
          children: <Widget>[
            Container(color: Colors.red,height: 100),
            Expanded(
              child: Container(color: Colors.purple,),
            ),
          ],
        ),
      )
    ],
  ),
)
AnasSafi
  • 5,353
  • 1
  • 35
  • 38