英文:
Only one item of data is coming from Firebase Firestore
问题
我写了这样的代码:
Container(
height: 250,
child: StreamBuilder(
stream: db.collection("DebrisPeoples").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("加载中");
} else {
final List<DebrisPeopleModel> data = snapshot.data!.docs
.map((e) => DebrisPeopleModel.fromDocument(e))
.toList();
inspect(data);
/*
输出:
[0]: DebrisPeopleModel
[1]: DebrisPeopleModel
[2]: DebrisPeopleModel
[3]: DebrisPeopleModel
[4]: DebrisPeopleModel
[5]: DebrisPeopleModel
*/
for (var item in data)
return Card(
child: ListTile(
title: Text(item.nameSurname.toString()),
),
);
}
return Container();
},
),
),
当我将数据打印到控制台时,数据显示出来,但当我将其投影到屏幕时,只有一个数据项显示。这可能是因为仅在for循环的第一次迭代中返回了Card小部件,导致只显示一个数据项。您可能需要将返回Card的逻辑移动到循环之外,以便将所有数据项都包含在Container中。如果需要显示所有数据项,您可以尝试以下更改:
return Card(
child: Column(
children: data.map((item) {
return ListTile(
title: Text(item.nameSurname.toString()),
);
}).toList(),
),
);
这将在一个Card小部件中显示所有数据项。
英文:
I wrote code like this:
Container(
height: 250,
child: StreamBuilder(
stream: db.collection("DebrisPeoples").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("yükleniyor");
} else {
final List<DebrisPeopleModel> data = snapshot.data!.docs
.map((e) => DebrisPeopleModel.fromDocument(e))
.toList();
inspect(data);
/*
OUTPUT:
[0]: DebrisPeopleModel
[1]: DebrisPeopleModel
[2]: DebrisPeopleModel
[3]: DebrisPeopleModel
[4]: DebrisPeopleModel
[5]: DebrisPeopleModel
*/
for (var item in data)
return Card(
child: ListTile(
title: Text(item.nameSurname.toString()),
),
);
}
return Container();
},
),
),
When I print it to the console, data shows up, but when I project it to the screen, only one item of data appears. Why could this be?
答案1
得分: 1
使用ListView.builder将Card
包装起来,而不是使用for循环
。
英文:
Wrap the Card
with a ListView.builder instead of using the for loop
.
答案2
得分: 1
尝试返回所有卡片,而不是单个项目,如下所示:
Container(
//height: 250,
child: StreamBuilder(
stream: db.collection("DebrisPeoples").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("加载中");
} else {
final List<DebrisPeopleModel> data = snapshot.data!.docs
.map((e) => DebrisPeopleModel.fromDocument(e))
.toList();
inspect(data);
List<Widget> cards = [];
for (var item in data) {
final card = Card(
child: ListTile(
title: Text(item.nameSurname.toString()),
),
);
cards.add(card);
}
return Column(children: cards); // 可能是ListView
}
},
),
)
英文:
Try to return all cards instead of single item like
Container(
//height: 250,
child: StreamBuilder(
stream: db.collection("DebrisPeoples").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text("yükleniyor");
} else {
final List<DebrisPeopleModel> data = snapshot.data!.docs
.map((e) => DebrisPeopleModel.fromDocument(e))
.toList();
inspect(data);
List<Widget> cards = [];
for (var item in data) {
final card = Card(
child: ListTile(
title: Text(item.nameSurname.toString()),
),
);
cards.add(card);
}
return Column(children: cards); // perhaps ListView
}
},
),
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论