【问题标题】:Widgets render in debug, but not in release小部件在调试中呈现,但不在发布中
【发布时间】:2021-07-23 11:41:19
【问题描述】:

在我的一个 Flutter 文件中,我有这段代码,理论上它似乎根本不应该是运行的问题。它在调试版本中很好地呈现,但是当我构建发布的 iOS 和 Android 应用程序文件时,它变得部分不可见。有一个名为 _heyName() 的小部件呈现,它只是一个文本小部件。 _whatWouldYouLike() 小部件几乎是 _heyName() 的 1:1 副本,但这个不渲染,之后也没有任何小部件。

控制台确实会抛出此警告:

======== Exception caught by widgets library =======================================================
The following assertion was thrown while applying parent data.:
Incorrect use of ParentDataWidget.

The ParentDataWidget Flexible(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 Flexible widget has the wrong ancestor RenderObjectWidget. Typically, Flexible widgets are placed directly inside Flex widgets.
The offending Flexible is currently placed inside a RepaintBoundary widget.

The ownership chain for the RenderObject that received the incompatible parent data was:
  ConstrainedBox ← Container ← Flexible ← RepaintBoundary ← IndexedSemantics ← NotificationListener<KeepAliveNotification> ← KeepAlive ← AutomaticKeepAlive ← KeyedSubtree ← SliverList ← ⋯
When the exception was thrown, this was the stack: 
#0      RenderObjectElement._updateParentData.<anonymous closure> (package:flutter/src/widgets/framework.dart:5723:11)
#1      RenderObjectElement._updateParentData (package:flutter/src/widgets/framework.dart:5739:6)
#2      RenderObjectElement.attachRenderObject (package:flutter/src/widgets/framework.dart:5761:7)
#3      RenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5440:5)
#4      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6082:11)
...
====================================================================================================
  Widget build(BuildContext context) {
    return Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(
              backgroundColor: Colors.transparent,
              shadowColor: Colors.transparent,
              pinned: false,
              expandedHeight: appBarHeight,
              automaticallyImplyLeading: false,
              floating: true,
              flexibleSpace: LayoutBuilder(
                builder: (context, constraints) {
                  return Container(
                    alignment: Alignment.bottomCenter,
                    child: Container(
                        height: SizeConfig.screenWidth * 0.13,
                        child: GestureDetector(
                            onTap: () {
                            },
                            child: _searchCategories(context))),
                  );
                },
              ),
            ),
          ];
        },
        body: ListView(
                  shrinkWrap: true,
                  children: [
                    _heyName(),
                    SizedBox(
                      height: 10,
                    ),
                    _whatWouldYouLike(),
                    SizedBox(
                      height: 10,
                    ),
                    _requests(),
                    SizedBox(
                      height: 10,
                    ),
                    Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _categoriesText(),
                            _seeAll(),
                          ],
                        ),
                        SizedBox(
                          height: SizeConfig.screenWidth * 0.03,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _category("Food and Beverages", 1),
                            _category("Design", 2),
                          ],
                        ),
                        SizedBox(
                          height: SizeConfig.screenWidth * 0.1,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _category("Music", 3),
                            _category("Sports and Fitness", 4),
                          ],
                        ),
                        SizedBox(
                          height: SizeConfig.screenWidth * 0.1,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _category("Personal Grooming", 5),
                            _category("Information Technology", 6),
                          ],
                        ),
                        SizedBox(
                          height: SizeConfig.screenWidth * 0.1,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _category("Home Services", 7),
                            _category("Lifestyle", 8),
                          ],
                        ),
                        SizedBox(
                          height: SizeConfig.screenWidth * 0.1,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _category("Pet Services", 9),
                            _category("Consulting", 10),
                          ],
                        ),
                        SizedBox(
                          height: SizeConfig.screenWidth * 0.25,
                        ),
                      ],
                    ),
                  ],
                ),
      ),
    );
  }
}

我可以在这里使用任何其他小部件来代替 ListView 吗?我觉得这是问题的根源,因为当我使用 Column 而不是 ListView 时,它渲染得很好,只是因为它比屏幕大,它会抛出旧的 RenderFlex 错误。我尝试将列放在扩展中,但没有运气。我觉得我在这里遗漏了一些东西。

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    这可能是由您的自定义小部件之一引起的,例如 _heyName() _whatWouldYouLike()。 共享这些小部件的代码,或者更好地查看您编写的代码,并确保它们不是以 Flexible 小部件开头。

    错误是不言自明的,除了RowColumn 之外,您还在其他东西中使用Flexible 小部件。因此,在您的 listView 中,子项以 Flexible 开头,因此您会收到此错误。 Expanded 也是如此,例如,您不能将 Expanded 放入容器或列表视图中。

    【讨论】:

    • 它是我提到的 _whatWouldYouLike() 小部件中的一个 Flexible。删除解决了这一切。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    相关资源
    最近更新 更多