0

我想使用颤振计算在 Firebase 上创建的总任务。有没有办法使用颤振来做到这一点我必须使用 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)=>未来

4

1 回答 1

1

您可以浏览每个文档并检查 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);
  }
}
于 2020-05-10T07:44:43.767 回答