使用提供方作为状态管理的流从firebase返回数据时遇到问题。
所有数据都是正确的,除了AppTemplate模型丢失了文档ID(id),我将其添加到模型中,您可以从下面的蒸汽中看到。在断点上
AppTemplate.fromSnapshot(data,documentId),
我可以看到它具有有效的ID
//Method to retrieve all template items from the same user based on uid
Stream<List<AppTemplate>> templatesStream() =>
_firestoreService.collectionStream(
path: FirestorePath.templates(uid),
builder: (data, documentId) =>
AppTemplate.fromSnapshot(data, documentId),
);
但是,当它使用下面的Id字段返回数据时为null。这意味着我无法以任何方式处理文档,即。删除/更新。
StreamBuilder(
stream: firestoreDatabase.templatesStream(),
initialData: new List<AppTemplate>(),
builder: (context, snapshot) {
if (snapshot.data != null) {
List<AppTemplate> templates = snapshot.data;
return ListView.builder(
physics:
const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemBuilder: (context, index) {
return ListTile(
title: Text(templates[index].title),
subtitle: Text(templates[index]
.description ==
null ||
templates[index].description ==
""
? "N/A"
: templates[index].description),
leading: Icon(Icons.collections),
trailing: GestureDetector(
onTap: () => {
firestoreDatabase
.deleteTemplate(
templates[index])
},
child: Icon(Icons.delete)),
);
},
itemCount: templates.length,
);
} else if (snapshot.hasError) {
return Center(child: Text('Error'));
}
return Center(
child: CircularProgressIndicator());
}),
这是我的应用模板模型:
class AppTemplate {
String id;
List<AppQuestion> questions;
String title;
String description;
Timestamp createdOn;
AppTemplate(
{this.id, this.questions, this.title, this.description, this.createdOn});
AppTemplate.fromData(Map<String, dynamic> json, String id) {
id = id ?? '';
;
title = json['title'];
description = json['description'];
createdOn = json['createdOn'];
if (json['questions'] != null) {
questions = new List<AppQuestion>();
json['questions'].forEach((v) {
questions.add(new AppQuestion.fromData(v));
});
}
}
AppTemplate.fromSnapshot(Map<String, dynamic> json, String id) {
id = id ?? '';
title = json['title'];
description = json['description'];
createdOn = json['createdOn'];
if (json['questions'] != null) {
questions = new List<AppQuestion>();
json['questions'].forEach((v) {
questions.add(new AppQuestion.fromData(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['description'] = this.description;
data['title'] = this.title;
data['createdOn'] = this.createdOn;
if (this.questions != null) {
data['questions'] = this.questions.map((v) => v.toJson()).toList();
}
return data;
}
}
预先感谢