0

I have tried to make each order unique by making the unique id the current date. Since the user can choose the same service multiple times, I want the repeated order to be written to the database as a different order.

I can't strong text make the child as current date. What is the problem? Kindly help me out.

onPressed: () {
final User _firebaseUser = firebaseAuth.currentUser;
    databaseReference
        .child("users")
        .child(_firebaseUser.uid)
        .child("orders")
        .child(DateTime.now().toString())
        .set({
          "service_type" : widget.serviceType.trim(),
          "service_price" : widget.servicePrice,
          "first_name" : firstNameController.text.trim(),
          "last_name" : lastNameController.text.trim(),
          "phone" : phoneController.text.trim(),
          "county" : countyNameController.text.trim(),
          "sub_county" : subCountyNameController.text.trim(),
          "exact_location" : _currentLocation.trim(),
        });
    showInSnackBar('Your order will be added in less thhan 1 hour');
    Navigator.push(context, MaterialPageRoute(builder: (context) => HomeScreen()),);
  },
                   

enter image description here

enter image description here

cngzz1
  • 143
  • 2
  • 9
Zuher Abud Said
  • 635
  • 2
  • 9
  • 16
  • What exactly do you expect it to do differently? Have you tried logging the value of `DateTime.now().toString()`? – Doug Stevenson Dec 24 '20 at 22:34
  • `print(DateTime.now().toString());` result is `[ ] I/flutter ( 6303): 2020-12-25 01:41:49.539140` – Zuher Abud Said Dec 24 '20 at 22:46
  • 1
    Surely firebase has a "here, keep this, and give it a unique ID" operation. DateTime is definitely not guaranteed to be unique. EDIT: yes, it does: https://stackoverflow.com/questions/28822054/firebase-how-to-generate-a-unique-numeric-id-for-key – Randal Schwartz Dec 24 '20 at 23:09
  • 1
    If you want a time-ordered key, you really should instead use `push()` to generate that. Then you can store the date values in a child value, if you want. – Doug Stevenson Dec 24 '20 at 23:15

2 Answers2

0

I'd rather say you use .toDate() method to convert a Timestamp to a Date.

Chukwuemeka Maduekwe
  • 6,687
  • 5
  • 44
  • 67
0

i have found an answer simply by changing to this code

.child(
    "${DateTime.now().day}" + 
    "${DateTime.now().month}" + 
    "${DateTime.now().year}" + 
    "${DateTime.now().timeZoneName}" + 
    "${DateTime.now().hashCode}"
  )

to uniquely identify an order

Zuher Abud Said
  • 635
  • 2
  • 9
  • 16
  • This is not true. Firebase does allow spaces in names of keys. The [documentation](https://firebase.google.com/docs/database/web/structure-data#how_data_is_structured_its_a_json_tree) will tell you which characters are invalid: "$, #, [, ], /, or ASCII control characters 0-31 or 127. You cannot use ASCII control characters in the values themselves, either." If you violate this, the write operation will generate an error telling you this as well. – Doug Stevenson Dec 24 '20 at 23:12
  • 1
    If you think the documentation is wrong, then file a bug with your specific findings to Firebase support. You should provide example code using hard-coded strings that clearly illustrate the problem. https://support.google.com/firebase/contact/support – Doug Stevenson Dec 25 '20 at 21:13
  • i am not sure whats the problem with my code why it gave me an error but it worked for me after i changed my code so i am just sharing with the other coders – Zuher Abud Said Dec 25 '20 at 21:20
  • I understand, and I'm saying it's not because of the whitespace as you said in the answer. – Doug Stevenson Dec 25 '20 at 21:49