You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中ListView.builder无法渲染内容,寻求问题排查方案

问题原因及解决方案

核心问题:ListView.builder在Column中缺少高度约束

当ListView.builder作为Column的子组件时,Column在垂直方向没有固定高度限制,而ListView默认会尝试占用无限垂直空间,这会导致Flutter无法正确计算ListView的布局尺寸,进而不会触发itemBuilder的执行逻辑。

具体修复方案

将FutureBuilder返回的ListView.builder用Expanded组件包裹,让它自动占用Column中剩余的可用垂直空间:

else if (snapshot.hasData) {
  List<Map<String, dynamic>> data = snapshot.data!;
  return Expanded( // 新增Expanded包裹ListView
    child: ListView.builder(
      itemCount: data.length,
      itemBuilder: (context, index) {
        print('entrou no builder');
        String title = data[index]['title'];
        String text = data[index]['description_text'];
        int id = data[index]['id'];
        return CardRoutine(id: id, title: title, postText: text);
      },
    ),
  );
}

额外排查点

  • 验证数据长度:在ListView.builder前打印data.length,确认API返回的列表确实包含数据。
  • 检查子组件布局:如果CardRoutine自身高度为0,也会出现"看似未渲染"的情况,但此时itemBuilder其实已经执行(可通过打印语句确认)。

内容的提问来源于stack exchange,提问作者Fernando Lucas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 18:05:22