Firebase 颤振/飞镖/火箭筒-流返回空列表
我遇到一个问题,在flutter中将流返回到StreamBuilder小部件。我试图访问一个自定义类,该类作为列表存储在firebase的users集合中,但由于某些原因,它继续返回空列表 下面是我的Firebase 颤振/飞镖/火箭筒-流返回空列表,firebase,flutter,dart,google-cloud-firestore,stream,Firebase,Flutter,Dart,Google Cloud Firestore,Stream,我遇到一个问题,在flutter中将流返回到StreamBuilder小部件。我试图访问一个自定义类,该类作为列表存储在firebase的users集合中,但由于某些原因,它继续返回空列表 下面是我的PhotoEntry自定义类的一个示例: class PhotoEntry { final Timestamp date; final String fileUrl; PhotoEntry({ @required this.date, @required this.fi
PhotoEntry
自定义类的一个示例:
class PhotoEntry {
final Timestamp date;
final String fileUrl;
PhotoEntry({
@required this.date,
@required this.fileUrl,
});
Map<String, dynamic> toMap() => {
'date': date.millisecondsSinceEpoch,
'fileUrl': fileUrl,
};
PhotoEntry.fromMap(Map<String, dynamic> data)
: date = new Timestamp.fromMillisecondsSinceEpoch(data['date']),
fileUrl = data['fileUrl'];
PhotoEntry.initial()
: date = Timestamp.now(),
fileUrl = 'No Photo';
}
我已经验证了集合和文档标题是否正确,传入的用户不为null,并且包含所有正确的数据。我在这里做错了什么?我也遇到过类似的情况,不确定这是否就是你的情况,但我的回答可以帮助那些面临空名单问题的人。在我的例子中,有一个用户集合,其中有一个医生集合,它保存他们的消息集合。 我通过firebase仪表板手动添加了一个医生对象作为字段,因此firebase无法将其作为集合访问,并且总是返回空列表。下图可能有助于更好地理解:
so
PhotoEntry.fromMap
被多次调用,但您有一个空列表?@pskinkPhotoEntry.fromMap
在将我的地图转换为列表时仅被调用一次。但是是的,它是一个空列表。您在哪里使用getuserphotostream
方法?发布您的StreamBuilder的代码
@pskink编辑了我的原始帖子以包含StreamBuilder
似乎itemCount:\u user.photos.length
不正确-您必须根据snaphot.data
Stream<List<PhotoEntry>> getUserPhotosStream(User user) {
if (user == null) {
return Stream.empty();
}
try {
return _db
.collection('users')
.document(user.uid)
.collection('photos')
.snapshots()
.map((list) => list.documents
.map((doc) => PhotoEntry.fromMap(doc.data))
.toList());
} catch (e) {
print(e);
rethrow;
}
}
Widget _galleryPage() {
return StreamBuilder<List<PhotoEntry>>(
stream: _db.getUserPhotosStream(_user),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting ||
!snapshot.hasData) {
return Container(child: Center(child: CircularProgressIndicator()));
} else {
return ListView.builder(
scrollDirection: Axis.horizontal,
physics: BouncingScrollPhysics(),
itemCount: _user.photos.length,
itemBuilder: (context, index) {
var photo = snapshot; // For debugging
print(photo); // For dubigging
// return Card(photo)
},
);
}
},
);
}