'How can I Store Document in chronological order in firebase with flutter

I am coding an appointment booking application I want to store my own created document in firestore database but in chronological order. So that I can see my latest booked appointment. Is there any way I can achieve this? I am attaching the piece of code for storing data in firebase. Please suggest some modifications to this code.

Future<void> userSetup() async {
    String user = FirebaseAuth.instance.currentUser?.displayName as String;
    DocumentReference<Map<String, dynamic>> users =
        FirebaseFirestore.instance.collection(user).doc();
    
    final hours = time?.hour.toString().padLeft(2, '0');
    final minutes = time?.minute.toString().padLeft(2, '0');
    users.set({
      "customerId": FirebaseAuth.instance.currentUser?.uid.toString(),
      "customerName": FirebaseAuth.instance.currentUser?.displayName,
      "customerEmail": FirebaseAuth.instance.currentUser?.email,
      "selectedTime": '${hours}:${minutes}',
      "selectedDate": DateFormat('dd/MM/yyyy').format(date!),
    });
    return;
  }


Solution 1:[1]

Fixed the issue just used the timestamp as the document name. I added this line to my code

String docId = (DateTime.now().millisecondsSinceEpoch).toString();

And finally, the complete code is:

Future<void> userSetup() async {
    String user = FirebaseAuth.instance.currentUser?.displayName as String;
    String docId = (DateTime.now().millisecondsSinceEpoch).toString();
    DocumentReference<Map<String, dynamic>> users =
        FirebaseFirestore.instance.collection(user).doc(docId);

    final hours = time?.hour.toString().padLeft(2, '0');
    final minutes = time?.minute.toString().padLeft(2, '0');
    users.set({
      "customerId": FirebaseAuth.instance.currentUser?.uid.toString(),
      "customerName": FirebaseAuth.instance.currentUser?.displayName,
      "customerEmail": FirebaseAuth.instance.currentUser?.email,
      "selectedTime": '${hours}:${minutes}',
      "selectedDate": DateFormat('dd/MM/yyyy').format(date!),
    });
    return;
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rudransh Singh Mahra