I am relatively new at Flutter and I am getting the following error message.
Error Message:
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 ParentData.
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 SizedBox widget.
The following is the codes which I believe the error lies:
Material(
elevation: 2,
color: Colors.white70,
child:
Padding(
padding: EdgeInsets.symmetric(
horizontal: size.width * 0.05,
vertical: size.height * 0.02,
),
child: SizedBox(
height: size.height * 0.4,
width: size.width,
child: Column(
children: <Widget>[
_orders
? StreamBuilder(
stream: FirebaseFirestore.instance
.collection('VendorOrders')
.doc(_uid)
.collection('CurrentOrders')
.orderBy('orderTime', descending: true)
.snapshots(),
builder: (context,
AsyncSnapshot<QuerySnapshot> orderSnapshot) {
if (orderSnapshot.connectionState ==
ConnectionState.waiting) {
return Container(
alignment: Alignment.center,
child: Center(
child: CircularProgressIndicator(
backgroundColor: colorTeal,
strokeWidth: 1,
),
),
);
} else {
//error is likely from here
return Expanded(
child: ListView.builder(
itemCount:
orderSnapshot.data.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot order =
orderSnapshot.data.docs[index];
if (current(order)) {
return VendorOrderDetailCard(
doc: order);
} else {
return Container();
}
}
)
);
}
})
: Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'There are no ongoing orders',
textAlign: TextAlign.center,
style: TextStyle(
color: colorDarkBlue,
fontFamily: 'Montserrat',
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
],
),
),
),
),
I think the error is due to the Expanded widget conflicting with the SizedBox but I am not sure what the workaround is. I tried to change up the expanded to flexible and sizedbox but to no avail. I would appreciate any sort of help!
CodePudding user response:
Expanded should be child of Row & Column
ListView.builder is auto Expanded and scrollable,
so no need to wrap ListView.builder by Expanded. you may try two portions of your code by below code snippet.
return Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(child: Text(
'There are no ongoing orders hfdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.amber,
fontFamily: 'Montserrat',
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),)
],
);
for list part
return ListView.builder(
itemCount:
orderSnapshot.data.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot order =
orderSnapshot.data.docs[index];
if (current(order)) {
return VendorOrderDetailCard(
doc: order);
} else {
return Container();
}
}
);
Note : if list data not showing you can set height of list builder wrapping with another content.
CodePudding user response:
You can just use Expanded
inside Row
, Column
and Flex
. Example:
Row(
children: [
Expanded(child: Text('Some text')),
Container(),
Expanded(child: Icon(Icons.add)),
]
);
So remove your misplaced Expanded
.