在 Flutter 中从 Firebase FireCloud 接收数据时,StreamBuilder 中的快照代码不会被执行
Posted
技术标签:
【中文标题】在 Flutter 中从 Firebase FireCloud 接收数据时,StreamBuilder 中的快照代码不会被执行【英文标题】:Snapshot code inside StreamBuilder does not get executed when receiving data from Firebase FireCloud in Flutter 【发布时间】:2020-12-24 03:36:53 【问题描述】:我正在尝试从 Firecloud 数据库中获取描述文本并在 3 个不同的小部件中使用它们,将这些小部件添加到列表中,然后将该列表传递给 ListView
。
为此,我在StreamBuilder
中执行了以下代码:
StreamBuilder(
stream: FirebaseFirestore.instance.collection('humansOfLpc').snapshots(),
builder: (BuildContext context,snapshot)
if(!snapshot.hasData)
return Center(
child: Text('Loading...'),
);
print('outside title and desc widget');
List<TitleAndDescription> titleAndDescriptions=[
];
snapshot.data.docs.map((document)
final about = document.data()["about"];
final aftercampusProgress =document.data()["afterCampusPlacement"];
final incampusPlacement = document.data()["incampusProgress"];
print('inside snapshots');
print(about);
final aboutDesc = TitleAndDescription(
title: 'About',
description: about,
);
final incampusProgressDesc = TitleAndDescription(
title: 'In-Campus Progress',
description: incampusProgress,
);
final afterCampusPlacementDesc=TitleAndDescription(
title: 'After Campus Placements',
description: afterCampusPlacement,
);
titleAndDescriptions.add(aboutDesc);
titleAndDescriptions.add(incampusProgressDesc);
titleAndDescriptions.add(afterCampusPlacementDesc);
);
return Expanded(
child: ListView(
children: titleAndDescriptions,
),
);
,
),
我面临的主要问题是它没有阅读以下代码:
snapshot.data.docs.map((document)
final about = document.data()["about"];
final aftercampusProgress =document.data()["afterCampusPlacement"];
final incampusPlacement = document.data()["incampusProgress"];
print('inside snapshots');
print(about);
final aboutDesc = TitleAndDescription(
title: 'About',
description: about,
);
final incampusProgressDesc = TitleAndDescription(
title: 'In-Campus Progress',
description: incampusProgress,
);
final afterCampusPlacementDesc=TitleAndDescription(
title: 'After Campus Placements',
description: afterCampusPlacement,
);
titleAndDescriptions.add(aboutDesc);
titleAndDescriptions.add(incampusProgressDesc);
titleAndDescriptions.add(afterCampusPlacementDesc);
);
我可以这样说,因为控制台正在打印 'outside title and desc widget'
但不打印 'inside snapshots'
。
因此,我的titleAndDescriptions
列表为空,我的 UI 上没有 3 个小部件和数据。请告诉我我做错了什么以及我有什么替代方案?
提前致谢。
【问题讨论】:
【参考方案1】:使用forEach
而不是map
snapshot.data.docs.forEach((document)
final about = document.data()["about"];
final aftercampusProgress =document.data()["afterCampusPlacement"];
final incampusPlacement = document.data()["incampusProgress"];
print('inside snapshots');
print(about);
final aboutDesc = TitleAndDescription(
title: 'About',
description: about,
);
final incampusProgressDesc = TitleAndDescription(
title: 'In-Campus Progress',
description: incampusProgress,
);
final afterCampusPlacementDesc=TitleAndDescription(
title: 'After Campus Placements',
description: afterCampusPlacement,
);
titleAndDescriptions.add(aboutDesc);
titleAndDescriptions.add(incampusProgressDesc);
titleAndDescriptions.add(afterCampusPlacementDesc);
);
在此处查看Why map doesn't work but forEach does。
【讨论】:
谢谢。这真的帮助了我。由于某些未知原因,map 不起作用,但 foreach 可以!以上是关于在 Flutter 中从 Firebase FireCloud 接收数据时,StreamBuilder 中的快照代码不会被执行的主要内容,如果未能解决你的问题,请参考以下文章
如何在flutter中从firebase数据填充listview
如何在 Flutter 中从 firebase 存储读取和写入文本文件?