Im writing data into Firestore DB, Im successfully writing it, as well then reading it based on TimeStamp order.
Now I would like to update TimeStamp of certain listId with current time plus 5 minutes, but can't figure it out, Was checking online, but only examples I was able to find were for other languages than dart, and couldn't figure it out.
Write Data
Future<void> addSentences(String listId) {
final Sentences = FirebaseFirestore.instance.collection('Sentences');
return Sentences.add({
'listId': listId,
'timeStamp': Timestamp.now(),
});
}
Get oldest listId
void getOldestListId() async {
await for (var messages
in FirebaseFirestore.instance.collection("Sentences").orderBy("timeStamp").limit(1).snapshots()) {
for (var message in messages.docs.toList()) {
print(message.data());
Map we = message.data();
print(we['listId']);
}
}
}
Tried to update timeStamp with TimeStamp.now() plus 5 minutes, but can't figure how to do it
void update() async {
var collection = FirebaseFirestore.instance.collection('sentences');
collection
.doc(listId) // <-- Doc ID where data should be updated.
.update(timeStamp: Timestamp.now() minutes(5));
}
EDIT
Please note that listId
is value Im saving into Firestore database and not document Id so it can't be inserted into .doc(listId)
, when I replace listId
with document Id I see in Firestore DB .doc('idCopiedFromFireBaseConsole'),
then I can update timeStamp with current time timeStamp, but still I don't know how to get document Id, I guess I should get document Id in getOldestListId()
function, how could I get proper Id there?
void getOldestListId() async {
await for (var messages
in FirebaseFirestore.instance.collection("Sentences").orderBy("timeStamp").limit(1).snapshots()) {
for (var message in messages.docs.toList()) {
print(message.data());
Map we = message.data();
print(we['listId']);
}
}
}
This is how I can update timeStamp with current time 5 minutes
void update() async {
var collection = FirebaseFirestore.instance.collection('sentences'); collection.doc('idCopiedFromFireBaseConsole').update({'timeStamp': DateTime.now().add(const Duration(minutes: 5))});
EDIT Now it works as I expected it to work, posting code for others with similar issue
Function to add data to FireStore
Future<void> addSentences(String sentenceId) {
final sentences = FirebaseFirestore.instance.collection('sentences');
return sentences.add({
'sentenceId': sentenceId,
'timeStamp': Timestamp.now(),
});
}
Loop in which addSentences()
is being used to add data to Firestore DB
for (int i = 0; i <= 3; i ) {
//_sentenceList.length
addChineseSentences(i.toString());
}
Function to get oldest sentenceId
late String _docId;
void getOldestSentence() async {
await for (var messages
in FirebaseFirestore.instance.collection("sentences").orderBy("timeStamp").limit(1).snapshots()) {
for (var message in messages.docs.toList()) {
_docId = message.id;
print(_docId);
Map we = message.data();
print(we['listId']);
}
}
}
Function to update sentence timeStamp
void updateSentence() async {
var collection = FirebaseFirestore.instance.collection('sentences');
collection.doc(_docId) // <-- Doc ID where data should be updated.
.update({'timeStamp': Timestamp.fromDate(DateTime.now().add(const Duration(minutes: 5)))});
}
CodePudding user response:
can you try with:
collection
.doc(listId) // <-- Doc ID where data should be updated.
.update(timeStamp: Timestamp.fromDate(DateTime.now().add(const Duration(minutes: 5))));
CodePudding user response:
That should be something like this:
collection
.doc(listId)
.update(timeStamp: Timestamp(Timestamp.now(). millisecondsSinceEpoch 5*60*1000);
The Timestamp.now(). millisecondsSinceEpoch
gives you the current time in milliseconds, then we add 5 minutes in milliseconds to it, and convert the combined value back into a Timestamp
.