在 Flutter 中将 Widget 置于 ListView 之上

Putting Widget above a ListView in Flutter

下面的代码在我调用和显示来自 firestore 的数据时运行良好:

return ListView(
 padding: const EdgeInsets.only(top: 20.0),
 children: snapshot.map((data) => _buildListItem(context, data)).toList(),
);

但是如果我试图把它放在一个列中所以我在它上面添加另一个小部件,它会失败并给出 stack overflow:

return Column(
    children: <Widget>[
    Text('Hellow'),
  ListView(
  padding: const EdgeInsets.only(top: 20.0),
  children: snapshot.map((data) => _buildListItem(context, data)).toList(),
)]);

您需要将 ListView 放入 Expanded 小部件中,以便它知道可以填充多少 space。

return Column(
    children: <Widget>[
      Text('Hellow'),
      Expanded(
        child: ListView(
          padding: const EdgeInsets.only(top: 20.0),
          children:
              snapshot.map((data) => _buildListItem(context, data)).toList(),
        ),
      ),
    ],
  );