I would like to calculate the total created task on firebase using flutter. Is there a way to do it using flutter do I have to use java and swift to do this function. This is how I stored my task and they are generated by firebase (ID).
Done it in realtime database
final tasksData = Provider.of<Tasks>(context);
final tasks = tasksData.tasks;
print(tasks.length); // total tasks
Future<void> fetchAndSetProducts() async {
const url = 'https://firebaselink.firebaseio.com/tasks.json';
try {
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
final List<TodoList> loadedTasks = [];
extractedData.forEach((taskId, taskData) {
loadedTasks.add(TodoList(
id: taskId,
name: taskData['name'],
description: taskData['description'],
date: DateTime.parse(taskData['date']),
filename: taskData['filename'],
location: taskData['location'],
isCompleted: taskData['isCompleted'],
notes: taskData['notes'],
));
});
_tasks = loadedTasks;
notifyListeners();
} catch (error) {
print(error);
throw (error);
}
}
List<TodoList> _tasks = [];
List<TodoList> get tasks {
return [..._tasks];
}
Further questions display future in Text widget
Future<void> refreshTasks(BuildContext context) async {
await Provider.of<Tasks>(context,listen: false).totalCompletedTask();
}
Text( refreshTasks.toString())
Error: Got this: Closure: (BuildContext) => Future