3

I'm new in mobile development and flutter, and I was trying to get data from firebase realtime database using flutter to use it in a list. Here's the database that i'm trying to retrieve data from.

Realtime Database

I wanna get all "timespent" putted into the database, in one list then return a string showing the total time (which is the sum of all "timespent" data).

String sumTimers() {
String sumstring;
int sumseconds = 0;
List<String> hms;
List<String> list;
List<int> listint;


FirebaseDatabase.instance
    .reference()
    .child("Timers")
    .once()
    .then((DataSnapshot snapshot) {
  Map<dynamic, dynamic> values = snapshot.value;
  values.forEach((key, values) {
    list.add(values["timespent"]);
  });
});

int c = 0;
for (var x in list) {
  hms = x.split(":");

  listint[c] = int.parse(hms[0]) * 60 * 60 +
      int.parse(hms[1]) * 60 +
      int.parse(hms[2]);
  c = c + 1;
  sumseconds = sumseconds + listint[c];
}
sumstring = secondstoString(sumseconds);

return sumstring;}

  

String secondstoString(int tseconds) {
    String timoo;
    if (tseconds < 60) {
      timoo = "00:00:" + tseconds.toString();
      tseconds = tseconds - 1;
    } else if (tseconds < 3600) {
      int m = tseconds ~/ 60;
      int s = tseconds - (60 * m);
      timoo = "00:" + m.toString() + ":" + s.toString();
      tseconds = tseconds - 1;
    } else {
      int h = tseconds ~/ 3600;
      int t = tseconds - (3600 * h);
      int m = t ~/ 60;
      int s = t - (60 * m);
      timoo = h.toString() + ":" + m.toString() + ":" + s.toString();
      tseconds = tseconds - 1;
    }

    return timoo;
  }

every time I debug my code I get this error:

Error

Which means that the list is still null and nothing was added to it and I don't understand why.

What am I doing wrong ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0MM
  • 39
  • 3

2 Answers2

0

The issue you're running into here is that in the first part of the code you're saying, "Hey, Firebase, please retrieve this data and then when you receive it, put it into this variable called list." But then you're immediately turning around and trying to process list, even before Firebase has had a chance to fill it up. So it's null at the point when you're trying to iterate on it.

There are a few ways of fixing this, but I suppose the simplest would simply be to add your list parsing logic into the then clause of your Firebase call. A little something like this...

Future<String> sumTimers() async {
  FirebaseDatabase.instance
    .reference()
    .child("Timers")
    .once()
    .then((DataSnapshot snapshot) {
  Map<dynamic, dynamic> values = snapshot.value;
  values.forEach((key, values) {
    list.add(values["timespent"]);
  });
  int c = 0;
  for (var x in list) {
    hms = x.split(":");
    ... etc ...
  }
  return sumstring;
});

Note that in doing so, you're going to need to mark your function as async and declare that it returns a Future that resolves to String. If that previous sentence sounds like mumbo-jumbo, I highly recommend watching this video on Dart Futures, which explains things nicely.

Todd Kerpelman
  • 16,875
  • 4
  • 42
  • 40
0

This DataSnapshot not yet become list :

Map<dynamic, dynamic> values = snapshot.value;

This will do :

Map<dynamic,dynamic> curentvalue = snapshot.value;
List mylist = curentvalue.values.tolist();

mylist.forEach((key, values) {
list.add(values["timespent"]);

});