首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不正确地使用ParentDataWidget。-我怎么解决这个问题?

不正确地使用ParentDataWidget。-我怎么解决这个问题?
EN

Stack Overflow用户
提问于 2022-03-09 08:30:55
回答 2查看 1.4K关注 0票数 1

我是比较新的颤振和我得到以下错误信息。

错误信息:

代码语言:javascript
运行
复制
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.

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

代码语言:javascript
运行
复制
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,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                    ],
                  ),
                ),
              ),
            ),

我认为这个错误是由于扩展的小部件与SizedBox冲突造成的,但我不知道解决方法是什么。我试着把扩充后的盒子改成灵活的大小箱,但没有用。任何帮助我都会感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-09 09:34:19

展开应该是行的子行&列

ListView.builder是自动扩展和可滚动的

因此,不需要通过展开包装ListView.builder。您可以通过下面的代码片段来尝试代码的两部分。

代码语言:javascript
运行
复制
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,
          ),
        ),)

      ],
    );

列表部件

代码语言:javascript
运行
复制
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();
          }
        }
    );

注意:如果列表数据没有显示,您可以设置列表生成器与其他内容包装的高度。

票数 4
EN

Stack Overflow用户

发布于 2022-03-09 09:17:28

您只需在RowColumnFlex中使用Column。示例:

代码语言:javascript
运行
复制
Row(
  children: [
     Expanded(child: Text('Some text')),
     Container(),
     Expanded(child: Icon(Icons.add)),
  ]
);

因此,移除您的错误的Expanded

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71406374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档