Nabia Salman
Nabia Salman

Reputation: 632

How to store data in Firebase Real-time Database under different date

I am making an App to save and display the seizure history of patients. As I am new to Flutter, I am having difficulty in how to store data in firebase for the current user at the time of seizure occurrence.

I am storing data in the real-time database, so for that, I am using the date as a child and if more than 1-time seizure has occurred then it will store the data with the new key under the same date, but let say if a seizure occurs tomorrow as well with date 3-6-2021, then how will it store the value with a new date?

Currently, data is storing like this:

enter image description here

How can I store data with the new date under the same child as Seizure_history?

My code looks like this:

// Store seizure data to firebase on occurrence
  storeSeizureData() {
    String dateString = DateTime.now().toString();
    var dateTime = DateTime.parse(dateString);
    var date = "${dateTime.day}-${dateTime.month}-${dateTime.year}";
    String duration = '15';
    print("Saving data to firebase");
    ref
        .child('User_data')
        .child(cuser.uid)
        .child("Seizure_history")
        .child(date)
        .push()
        .set({'duration': duration, 'datetime': dateString});

    Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(
        builder: (context) {
          return Homepage(device: widget.device);
        },
      ),
      (route) => false,
    );
  }

I want that next day seizure should be stored separately with the new date and not under the previous date, i want to store like this:

{
  "User_data" : {
    "QURxPw4UwBhvn0ZdMYb1wNPJ9ZH2" : {
      "Seizure_history" : {
        "2-6-2021" : {
          "-Mb8E79qdLq2-EI3Vy0i" : {
            "datetime" : "2021-06-02 01:20:56.535240",
            "duration" : "15"
          },
          "-Mb8EA3TYOQOUVpzQ3aE" : {
            "datetime" : "2021-06-02 01:21:08.509808",
            "duration" : "15"
          }
        }
        "3-6-2021" : {
          "-Mb8E79qdLq2-EI3Vy0i" : {
            "datetime" : "2021-06-02 01:20:56.535240",
            "duration" : "15"
          },
          "-Mb8EA3TYOQOUVpzQ3aE" : {
            "datetime" : "2021-06-02 01:21:08.509808",
            "duration" : "15"
          }
        }
      },
      "caregivers" : {
        "-Mat_n4SPyg3mgAFJeYi" : {
          "Caregiver_Name" : "Bro 😝",
          "Caregiver_Number" : "033-253-04108 "
        }
      },
      "dateBirth" : "1999-05-26",
      "email" : "[email protected]",
      "gender" : "female",
      "mobile_no" : "2o29238347",
      "name" : "Nabia Salman",
      "profile_photo" : "https://firebasestorage.googleapis.com/v0/b/epicare-4d900.appspot.com/o/image_picker5066472271726518955.jpg?alt=media&token=41b83fa6-4429-448d-8b1b-48549efacba8"
    },
    "SmnClTT1STO3HywzhfZed1EF8GC2" : {
      "caregivers" : {
        "-MaXrew6oMLNIbpLdL9r" : {
          "Caregiver_Name" : "Dad",
          "Caregiver_Number" : "03145107020 "
        },
        "-MaXrhdR3BB4CVm_cRMN" : {
          "Caregiver_Name" : "Mom",
          "Caregiver_Number" : "03145308797 "
        },
        "-MaXtqrOMw0QLGdKkndG" : {
          "Caregiver_Name" : "Sister Zong",
          "Caregiver_Number" : "03176037020 "
        }
      },
      "dateBirth" : "2021-05-12",
      "email" : "[email protected]",
      "gender" : "leamef",
      "mobile_no" : "875487248944",
      "name" : "Hania Salman",
      "profile_photo" : "https://firebasestorage.googleapis.com/v0/b/epicare-4d900.appspot.com/o/c76d8746-395c-4632-82c3-709a8439f6c76494573163837145403.jpg?alt=media&token=0b425643-0e53-433b-aa19-a61898b4265f"
    },
    "yh6ATxquKjbmIWTbhVBudrGBVKB3" : {
      "caregivers" : {
        "-MaUwABpcS_MD6OLmaLP" : {
          "Caregiver_Name" : "Dad",
          "Caregiver_Number" : "03145107020 "
        }
      },
      "dateBirth" : "2021-05-21",
      "email" : "[email protected]",
      "gender" : "male",
      "mobile_no" : "03325304108",
      "name" : "Abdurehman",
      "profile_photo" : "https://firebasestorage.googleapis.com/v0/b/epicare-4d900.appspot.com/o/image_picker-2053800923.jpg?alt=media&token=8232ffd0-bbb7-4090-99f6-10526d0de292"
    }
  }
}

Upvotes: 1

Views: 1561

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

Since you start with:

String dateString = DateTime.now().toString();

Any time this code runs, it determines the current date. So if you run the code on June 3, it will generate that date, and the key in your database will be for June 3.


Unrelated to your actual question, but I strongly recommend using ISO-8601 format for the dates, which would be: "2021-06-02". The advantage of this format, is that you can query for date ranges, for example getting all dates in 2021 with ref.orderByKey().startAt("2021-").endAt("2021~").

Upvotes: 1

Related Questions