170

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
  • 540
  • 2
  • 5
  • 27
Mounika
  • 2,428
  • 5
  • 14
  • 24
  • 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

10 Answers10

319

Expanded cannot be used inside a Stack.

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

iDecode
  • 12,616
  • 7
  • 58
  • 106
Mark Bell
  • 3,370
  • 1
  • 7
  • 14
66

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
  • 3,938
  • 7
  • 44
  • 71
secreal
  • 785
  • 5
  • 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
  • 10
    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
23

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
John Alexander Betts
  • 4,008
  • 7
  • 44
  • 69
Akbar Masterpadi
  • 454
  • 4
  • 11
  • 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() ) )` – eriel marimon Jun 02 '22 at 18:34
3

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
  • 160
  • 2
  • 14
  • 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
2

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

M.AQIB
  • 242
  • 2
  • 10
1

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

mjukka
  • 21
  • 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
0

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

}

0

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
  • 41
  • 5
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
  • 79
  • 1
  • 10
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
  • 181
  • 2
  • 6