【问题标题】:How do I get a query from another file to return a bool?如何从另一个文件中获取查询以返回布尔值?
【发布时间】:2022-01-22 16:41:44
【问题描述】:

我正在将以下系统集成到我的待办事项应用程序中: 每次用户打开应用程序时,它应该检查存储在 Cloud Firestore 中的日期是否已超过。 如果是这种情况,则用户的所有待办事项都应重置为 false。

这是我在 Cloud Firestore 中寻找的日期:

此函数应检查是否已超过日期:

Future<bool> checkTime() async{
    DateTime now = DateTime.now();
    var query = users.where('Startdatum', isLessThanOrEqualTo: now);
    query = query.where('userID', isEqualTo: userID);
    final querySnapshot = await query.get();
    return querySnapshot.size > 0;
  }

并且这个函数应该将所有待办事项重置为 false:

Future allFalse() async{
    return await users.doc(userID).get().then((DocumentSnapshot doc) {
      var updateMap = new Map();
      var toDos = doc['Level'];
      for (var item in toDos.keys) {
        updateMap[item] = false;
      }
      doc.reference.update({'Level' : updateMap});
    });
  }

我在一个单独的文件(数据库)中创建了这两个函数,您可以在此处看到:

class DatabaseService {
  String userID;

  DatabaseService(this.userID);

  final CollectionReference users =
  FirebaseFirestore.instance.collection('users');

  Future allFalse() async {
    return await users.doc(userID).get().then((DocumentSnapshot doc) {
      var updateMap = new Map();
      var toDos = doc['Level'];
      for (var item in toDos.keys) {
        updateMap[item] = false;
      }
      doc.reference.update({'Level': updateMap});
    });
  }

  Future<bool> checkTime() async {
    DateTime now = DateTime.now();
    var query = users.where('Startdatum', isLessThanOrEqualTo: now);
    query = query.where('userID', isEqualTo: userID);
    final querySnapshot = await query.get();
    return querySnapshot.size > 0;
  }
}

我在初始状态下创建了一个包含 checkTime 的 if 条件。如果 checkTime 返回 true,Future 返回 allFalse,这会将所有 To-Dos 设置为 false。

class _UebersichtState extends State<Uebersicht> {
  User? user;
  late DatabaseService database;

  Future<void> connect() async{
    final FirebaseAuth auth = FirebaseAuth.instance;
    UserCredential result = await auth.signInAnonymously();
    user = result.user;
    database = DatabaseService(user!.uid);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    connect();
      Future.delayed(Duration(seconds: 3), () async{
        if(await database.checkTime()) {
          return await database.allFalse();}
        else print('Still time left');
      });
    }

我使用了延迟,因为必须先执行 connect() 函数,它会初始化数据库。


当我启动应用程序时,没有出现错误,但待办事项也没有重置。

今天我们有 21. 12. 2021 和 Cloud Firestore 中的 14. 12. 21 已存入。 函数 allFalse 有效,它重置所有待办事项。 它必须是函数 checkTime,它在 if 条件下不返回布尔值。我只是用 if (0 == 0) 替换它,这会触发 allFalse。 有人可以帮我吗?

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    这只是一个猜测,但我相信这是问题所在:

    query = query.where('userID', isEqualTo: userID);
    

    只有当您的文档有一个字段 userID 并且该字段等于您的 UID 时,上述行才有效,但据我所知,如果是这种情况,您可以通过文档名称识别 UID ,这应该可以吗?

    Future<bool> checkTime() async {
      CollectionReference users = FirebaseFirestore.instance.collection('users');
      final now = DateTime.now();
      final doc = await users.doc(userID).get();
      final stufenzeit = (doc.data() as Map<String, dynamic>)['Stufenzeit'] as Map<String, dynamic>;
      final storedDate = (stufenSetit['Startdatum'] as TimeStamp).toDate();
      
      return now.compareTo(storedDate) > 0;
    }
    

    可能还有一种方法可以处理查询,但老实说,我不太精通这些。

    【讨论】:

    • 看起来不错的解决方案,但是doc后面的data()以红色高亮显示,控制台出现如下错误:Error: The method 'data' isn't defined for the class 'Future&lt;DocumentSnapshot&lt;Object?&gt;&gt; Function([GetOptions?])'.
    • 这很奇怪,代码对我有用。您确定在final doc = await users.doc(userID).get(); 这一行中添加了括号吗?确保你没有写错final doc = await users.doc(userID).get;
    • 这正是我犯的错误。现在没有红色下划线。当我现在启动应用程序时,它会正常启动,但未重置待办事项。然后控制台将显示:E/flutter ( 3935): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'Null' is not a subtype of type 'Timestamp' in type cast
    • 我认为这可能是我的错,因为我忘记了 Stufenzeit,我更新了我的答案,你能看看它现在是否有效吗?
    • 成功了,谢谢!
    猜你喜欢
    • 2011-07-02
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多