gpt4 book ai didi

flutter - 错误使用 ParentDataWidget。 - 我该如何解决?

转载 作者:行者123 更新时间:2023-12-02 15:56:43 27 4
gpt4 key购买 nike

我是 Flutter 的新手,收到以下错误消息。

错误信息:

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.

以下是我认为错误的代码:

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

我认为错误是由于 Expanded 小部件与 SizedBox 冲突造成的,但我不确定解决方法是什么。我试图将 expanded 更改为 flexible 和 sizedbox 但无济于事。我将不胜感激任何形式的帮助!

最佳答案

Expanded 应该是 Row & Column 的子元素
ListView.builder 是自动展开和滚动的,
所以不需要用Expanded 包裹ListView.builder。您可以通过以下代码片段尝试代码的两部分。

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

],
);

列表部分

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();
}
}
);

注意:如果列表数据未显示,您可以设置用其他内容包裹的列表构建器的高度。

关于flutter - 错误使用 ParentDataWidget。 - 我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71406374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com