根据 id flutter 计算在 firebase 上创建的总任务
Caculate the total task created on firebase base on id flutter
我想使用 flutter 计算在 firebase 上创建的任务总数。有没有一种方法可以使用 flutter 来实现,我必须使用 java 和 swift 来实现这个功能吗?这就是我存储任务的方式,它们是由 firebase (ID) 生成的。
在实时数据库中完成
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];
}
进一步的问题在文本小部件中显示未来
Future<void> refreshTasks(BuildContext context) async {
await Provider.of<Tasks>(context,listen: false).totalCompletedTask();
}
Text( refreshTasks.toString())
错误:
明白啦:
关闭:(BuildContext) => 未来
您可以查看每个文档并检查 isCompleted 是否为真,如果为真,则增加 totalTrue 变量的值,该变量将保存总的真实计数。
以下方法对您有用。
getCount() async {
int totalcount = 0;
await FirebaseDatabase.instance
.reference()
.child("tasks")
.once()
.then((DataSnapshot snapshot) {
Map<dynamic, dynamic> data = snapshot.value;
data.forEach((key, value) {
if (value['isCompleted']) {
totalcount++;
}
});
});
print(totalcount);
}
更新:
Future<int> fetchAndSetProducts() async {
const url = 'https://firebaselink.firebaseio.com/tasks.json';
try {
int totalcount = 0;
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
extractedData.forEach((taskId, taskData) {
if (taskData['isCompleted']) {
totalcount++;
}
});
print(totalcount);
return totalcount;
} catch (error) {
print(error);
throw (error);
}
}
我想使用 flutter 计算在 firebase 上创建的任务总数。有没有一种方法可以使用 flutter 来实现,我必须使用 java 和 swift 来实现这个功能吗?这就是我存储任务的方式,它们是由 firebase (ID) 生成的。
在实时数据库中完成
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];
}
进一步的问题在文本小部件中显示未来
Future<void> refreshTasks(BuildContext context) async {
await Provider.of<Tasks>(context,listen: false).totalCompletedTask();
}
Text( refreshTasks.toString())
错误: 明白啦: 关闭:(BuildContext) => 未来
您可以查看每个文档并检查 isCompleted 是否为真,如果为真,则增加 totalTrue 变量的值,该变量将保存总的真实计数。
以下方法对您有用。
getCount() async {
int totalcount = 0;
await FirebaseDatabase.instance
.reference()
.child("tasks")
.once()
.then((DataSnapshot snapshot) {
Map<dynamic, dynamic> data = snapshot.value;
data.forEach((key, value) {
if (value['isCompleted']) {
totalcount++;
}
});
});
print(totalcount);
}
更新:
Future<int> fetchAndSetProducts() async {
const url = 'https://firebaselink.firebaseio.com/tasks.json';
try {
int totalcount = 0;
final response = await http.get(url);
final extractedData = json.decode(response.body) as Map<String, dynamic>;
extractedData.forEach((taskId, taskData) {
if (taskData['isCompleted']) {
totalcount++;
}
});
print(totalcount);
return totalcount;
} catch (error) {
print(error);
throw (error);
}
}