Incorrect use of parent data widget. expanded widgets must be placed inside flex widgets
FlutterDartFlutter Problem Overview
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.
Flutter Solutions
Solution 1 - Flutter
Expanded
cannot be used inside a Stack
.
You should use Expanded
only within a Column
, Row
or Flex
Solution 2 - Flutter
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
Solution 3 - Flutter
there are may things to get this problem
- Under
ListView
don't useSpacer
Widget - don't use
Positioned
underRow
orColumn
Expanded
can only use it must be a descendant ofColumn
Row
Flex
Solution 4 - Flutter
there's no expanded in my code so i found that using Spacer()
too can cause this error if it's inside a listview
Solution 5 - Flutter
In my case, I was using Positioned
inside a column. Removing it solved the problem!
Solution 6 - Flutter
I removed a Positioned
widget that was inside a Container
inside a Stack
and the problem "Incorrect use of ParentDataWidget" went away.
Solution 7 - Flutter
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),
],
),
],
),
),
),
);
}
Solution 8 - Flutter
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
Solution 9 - Flutter
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.
Solution 10 - Flutter
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.