当前位置: 首页 > article >正文

Flutter Spacer引发的The ParentDataWidget Expanded(flex: 1) 惨案

问题描述

在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 StackParentData.

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 Stack widget.

The ownership chain for the RenderObject that received the incompatible parent data was:
  SizedBox.shrink ← Expanded ← Spacer ← Stack ← ConstrainedBox ← Padding ← Container ← Listener ← _GestureSemantics ← RawGestureDetector ← ⋯

问题分析

网上千篇一律的错误信息,都说是Expanded和Stack的关系问题。
但是排查代码中却没有任何地方使用到Expanded,而且由于报错没有指出在哪个页面,所以,问题一时间僵住了。

最后,通过一个一个注释代码块,确定问题大致代码块,最后按照SizedBox.shrink ← Expanded ← Spacer ← Stack ← ConstrainedBox ← Padding ← Container ← Listener ← _GestureSemantics ← RawGestureDetector ← ⋯顺序
一个一个在页面中排查,排查到Spacer,点击进去查看实现,Spacer的会当场build一个Expanded。

Spacer的源码如下:

class Spacer extends StatelessWidget {
  /// Creates a flexible space to insert into a [Flexible] widget.
  ///
  /// The [flex] parameter may not be null or less than one.
  const Spacer({super.key, this.flex = 1})
    : assert(flex > 0);

  /// The flex factor to use in determining how much space to take up.
  ///
  /// The amount of space the [Spacer] can occupy in the main axis is determined
  /// by dividing the free space proportionately, after placing the inflexible
  /// children, according to the flex factors of the flexible children.
  ///
  /// Defaults to one.
  final int flex;

  
  Widget build(BuildContext context) {
    return Expanded(
      flex: flex,
      child: const SizedBox.shrink(),
    );
  }
}

可以看到Spacer的build方法中,直接返回了一个Expanded,而Expanded的child是SizedBox.shrink()。这就是问题的根源。

解决方案

找到问题所在的页面,将Expanded和Spacer的关系注释掉,或者将Expanded的child改为Container,或者将Expanded的child改为Container()。

总结

不仅 Expanded 的使用限制在Flex/Row/Column中,Spacer 也有一样的限制。


http://www.kler.cn/news/306594.html

相关文章:

  • Spring Boot 注解探秘:JSON 处理的魔法世界
  • JAVA基础:线程优先级和精灵线程
  • 股指期货的详细玩法功能与应用解析
  • 详解 Pandas 的累计统计函数
  • Java后端开发(十七)-- Java中对Object、Collection集合、Map集合、数组进行判空
  • 数据结构:时间复杂度与空间复杂度
  • 【代码随想录训练营第42期 Day57打卡 - 图论Part7 - Prim算法与Kruskal算法
  • 后端开发刷题 | 数字字符串转化成IP地址
  • 状态机按键消抖(学习笔记)
  • Flink有界流实现(1)
  • 【Python】谷歌浏览器总是自动更新,使用selenium跟chromedriver版本不匹配怎么办?
  • 01,大数据总结,zookeeper
  • 算法练习题27——疫情下的电影院(模拟)
  • AI辅助癌症诊断取得了进展
  • Angular面试题一
  • 大模型 LLM(Large Language Models)如今十分火爆,对于初入此领域的新人小白来说,应该如何入门 LLM 呢?是否有值得推荐的入门教程呢?
  • 深度学习自编码器 - 引言篇
  • java基于PDF底层内容流的解析对文本内容进行编辑
  • 象过河手机进销存,外出办公更方便,随时了解经营情况
  • C# 静态static
  • 基于HTML5的下拉刷新效果
  • 如何避免长距离遗忘问题
  • HarmonyOS NEXT 封装实现好用的网络模块(基于最新5.0的API12)
  • Android 12 Launcher3 去掉Hotseat
  • JVM 调优篇7 调优案例3- gc overhead limit exceed
  • ListBox显示最新数据、左移和右移操作
  • K8s中HPA自动扩缩容及hml
  • idea2024.2永久使用
  • MFC工控项目实例之十五定时刷新PC6325A模拟量输入
  • HTML添加文字