Nabia Salman
Nabia Salman

Reputation: 632

How to retrieve data in Firebase Real-time Database

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 retrieve data from firebase for the current user for the date when selected from the calendar.

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. I want to fetch this data for the date as it is stored in firebase like this:

enter image description here

I am using this code to fetch the data for the selected date. By default, the date selected on the calendar is the current date. As this code fetches the data from the database correctly but it is not showing the other condition if the date has no data in firebase.

UPDATED CODE:

init state:

  @override
  void initState() {
    var dateTime = DateTime.now();
    _date = "${dateTime.day}-${dateTime.month}-${dateTime.year}";
    //call the load method
    loadData();

    setState(() {
      _controller = CalendarController();
    });
  }

load Data:

 loadData() {
    list.clear();
    print(_date);
    //load the data from firebase and add to the list
    fb.reference()
      ..child(cuser.uid)
          .child('Seizure_history')
          .child(_date)
          .once()
          .then((DataSnapshot snapshot) {
        var data = snapshot.value;
        data.forEach((key, value) {
          EventList seizure_history = new EventList(
            seiz_duration: value['duration'],
            dateTime: value['datetime'],
            key: key,
          );
          list.add(seizure_history);
        });
        setState(() {
          print("History Present");
        });
      });

  }

Calendar :

onDaySelected: (date, events, _) {
                    //print(TimeOfDay.fromDateTime(date));
                    print(date);
                    print(_focusedDay);
                    var datesel = "${date.day}-${date.month}-${date.year}";
                    //set the date
                    setState(() {
                      _date = datesel.toString();
                      //call the method again
                      loadData();
                    });

                  },

Whichever date I select it gives me the same seizure history on every date, I think the problem is with my date but I cannot figure out how can I make it global in order to change it on selection from the calendar

This is what it shows on every date:

enter image description here

Since DB only has data for 2-6-2021, but it shows the same data on every date. However, it should display this in case the date has no data in Firebase DB: (The below picture is UI design)

enter image description here

Upvotes: 0

Views: 559

Answers (1)

Jorge Vieira
Jorge Vieira

Reputation: 3062

You are loading the data inside the initState so it is loaded and listening that child one time only. Better you create a method to load the data and call it everytime the date changes.

loadData(){
  //first clear the list once you dont need the other result
  list.clear();
  
  //load the data from firebase and add to the list
  //i think you could use once(), cause you dont need you listen every date
  fb.reference()
      ..child(cuser.uid).child('Seizure_history').child(_date).once();

 //then you add the result to the cleared list remember call setstate to reload the ui
}

Then on the initState method you set the initial data and call the load method:

  @override
  void initState() {
    var dateTime = DateTime.now();
    //use the global variable instead of creating a local one
    _date = "${dateTime.day}-${dateTime.month}-${dateTime.year}";
    
   //call the load method
   loadData();
  }

Then when change the date on the calendar you set the new day and call the load method again.

onDaySelected: (date, events, _) {
   //print(TimeOfDay.fromDateTime(date));
   print(date);
   print(_focusedDay);
   var datesel = "${date.day}-${date.month}-${date.year}";
   
   //set the date
   _date = datesel.toString();
   //call the method again
   loadData();
},

I put only part of the code with the idea, hope you can make it works.

Upvotes: 1

Related Questions