我尝试了以下方法,但是它返回一个在Firestore中不存在的随机字符串。
我成功地使用查询快照获取了父集合的文档ID。
DocumentReference doc_ref=Firestore.instance.collection("board").document(doc_id).collection("Dates").document();
var doc_id2=doc_ref.documentID;
更多代码:我试图访问文档id时出现错误。我尝试使用await,但它会报错。
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text(
"National Security Agency",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.normal,
fontSize: 24.0),
),
backgroundColor: Colors.redAccent,
centerTitle: true,
actions: <Widget>[
new DropdownButton<String>(
items: <String>['Sign Out'].map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) => logout(),
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
},
child: Icon(Icons.search),
),
body: StreamBuilder (
stream: cloudfirestoredb,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((document) {
var doc_id=document.documentID;
var now= new DateTime.now();
var formatter=new DateFormat('MM/dd/yyyy');
String formatdate = formatter.format(now);
var date_to_be_added=[formatdate];
DocumentReference doc_ref=Firestore.instance.collection("board").document(doc_id).collection("Dates").document();
var doc_id5= await get_data(doc_ref);
print(doc_id);
Firestore.instance.collection("board").document(doc_id).collection("Dates").document(doc_id5).updateData({"Date":FieldValue.arrayUnion(date_to_be_added)});
return cardtemplate(document['Name'], document['Nationality'], doc_id);
}).toList(),
);
},
),
);
}