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.
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:
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 ?