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

标签 flutter dart mobile

我是 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/

相关文章:

android - 我应该使用哪种通知,推送通知还是本地通知?

Flutter:我可以在 ListView 构建器中启用/禁用 InkWell 吗?

dart - 如何禁用选项卡中的特定选项卡才能单击?

dart - 如何检测 Flutter App 代码中的热重载?

google-app-engine - 是否有任何官方计划在 Google App Engine 上支持 Dart?

flutter - Flutter-如何在加载UI之前动态添加容器的高度值?

javascript - PHP 网站为桌面和移动浏览器返回不同的 SHA512 值

flutter - Flutter 的 CupertinoSlidingSegmentedControl 的设计问题?

dart - Dart 程序中的拼写检查

html - Web 链接在移动显示上不起作用