I have tried the following but it returns a random string which is not present in the firestore.
I did manage to get documentid of parent collection using a Query Snapshot
DocumentReference doc_ref=Firestore.instance.collection("board").document(doc_id).collection("Dates").document();
var doc_id2=doc_ref.documentID;
More Code: I am getting an error in trying to access the document id. I have tried to use await but its giving an error.
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(),
);
},
),
);
}
You have to retrieve that document for its id.
Try this
DocumentReference doc_ref=Firestore.instance.collection("board").document(doc_id).collection("Dates").document();
DocumentSnapshot docSnap = await doc_ref.get();
var doc_id2 = docSnap.reference.documentID;
Make sure you use this in a function marked as async since the code uses await.
Edit : To answer your question in the comments
Future<String> get_data(DocumentReference doc_ref) async {
DocumentSnapshot docSnap = await doc_ref.get();
var doc_id2 = docSnap.reference.documentID;
return doc_id2;
}
//To retrieve the string
String documentID = await get_data();
Edit 2 :
Just add async to the map function.
snapshot.data.documents.map((document) async {
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(),
let me know if this works