Flutter 进入 Firebase 中的子子集合。

huangapple go评论72阅读模式
英文:

Flutter entering to sub-sub collection in firebase

问题

我的嵌套集合看起来是这样的:

User--> drugs--> drugsTime

我想要更新子子集合(drugsTime)中文档中的数据,但我不知道药物文档的ID和药物时间文档的ID,所以我做了以下操作:

ConfirmDrugTaken(DateTime d) async {
    final User? user = FirebaseAuth.instance.currentUser;
    final _uid = user?.uid;
    var drugID = "";
    
    // 获取Firestore中用户的子集合
    final QuerySnapshot<Map<String, dynamic>> querySnapshot =
    await FirebaseFirestore.instance
        .collection("users")
        .doc(_uid)
        .collection('drug')
        .where("drugName", isEqualTo: widget.title)
        .get();
    
    querySnapshot.docs.forEach((element) async {
        drugID = element.id;
    });
    
    final QuerySnapshot<Map<String, dynamic>> querySnapshot2 = await FirebaseFirestore.instance
        .collection("users")
        .doc(_uid)
        .collection("drug")
        .doc(drugID)
        .collection("drugDates")
        .where('time', isEqualTo: d)
        .get() as QuerySnapshot<Map<String, dynamic>>;
    
    querySnapshot2.docs.forEach((element) async {
        FirebaseFirestore.instance
            .collection("users")
            .doc(_uid)
            .collection("drug")
            .doc(drugID)
            .collection("drugDates")
            .doc(element.id)
            .update({'isTaken': true});
    });
}

但是出现了以下错误:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Future<QuerySnapshot<Map<String, dynamic>>>' is not a subtype of type 'QuerySnapshot<Map<String, dynamic>>' in type cast
E/flutter (17976): #0 _drugSetupState.ConfirmDrugTaken (package:taafi_application_v1/pages/drug_setup.dart:215:79)
E/flutter (17976):
E/flutter (17976): #1 _drugSetupState.setAlarm.... (package:taafi_application_v1/pages/drug_setup.dart:112:20)
E/flutter (17976):

是否有另一种方法可以进入子子集合而不知道ID,或者我如何修复这个错误?

请帮助。

英文:

my nested collections looks like this:

User--&gt; drugs--&gt; drugsTime

I want to update data in document in the subsub-collection (drugsTime)
however I dont know the drug document id and the drugtime document id
so I did the following :

 ConfirmDrugTaken(DateTime d) async {
    final User? user = FirebaseAuth.instance.currentUser;
    final _uid = user?.uid;
    var drugID=&quot;&quot;;
    // Get the user&#39;s sub-collections in Firestore
    final QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt; querySnapshot =
    await FirebaseFirestore.instance
        .collection(&quot;users&quot;)
        .doc(_uid)
        .collection(&#39;drug&#39;).where(&quot;drugName&quot;, isEqualTo: widget.title)
        .get();
    querySnapshot.docs.forEach((element) async {
      drugID=element.id;
    });

    final QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt; querySnapshot2 =FirebaseFirestore.instance
        .collection(&quot;users&quot;)
        .doc(_uid)
        .collection(&quot;drug&quot;)
        .doc(drugID).collection(&quot;drugDates&quot;).where(&#39;time&#39;,isEqualTo: d).get() as QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;;
    querySnapshot2.docs.forEach((element) async {
      FirebaseFirestore.instance
          .collection(&quot;users&quot;)
          .doc(_uid)
          .collection(&quot;drug&quot;)
          .doc(drugID).collection(&quot;drugDates&quot;).doc(element.id).update( {&#39;isTaken&#39;: true});

    });


  } 

but this error occur:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type &#39;Future&lt;QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;&gt;&#39; is not a subtype of type &#39;QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;&#39; in type cast
E/flutter (17976): #0      _drugSetupState.ConfirmDrugTaken (package:taafi_application_v1/pages/drug_setup.dart:215:79)
E/flutter (17976): &lt;asynchronous suspension&gt;
E/flutter (17976): #1      _drugSetupState.setAlarm.&lt;anonymous closure&gt;.&lt;anonymous closure&gt;.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:taafi_application_v1/pages/drug_setup.dart:112:20)
E/flutter (17976): &lt;asynchronous suspension&gt;

Is there is another way to enter to the sub-sub-collection with out knowing the id OR how can I fix this error?

please help

答案1

得分: 0

你忘记使用await来调用异步方法,代码应该如下所示,针对querySnapshot2

final QuerySnapshot<Map<String, dynamic>> querySnapshot2 = (await FirebaseFirestore.instance
        .collection("users")
        .doc(_uid)
        .collection("drug")
        .doc(drugID)
        .collection("drugDates")
        .where('time', isEqualTo: d)
        .get()) as QuerySnapshot<Map<String, dynamic>>;
英文:

You forgot to use await to call async method, code should be as follows for querySnapshot2

final QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt; querySnapshot2 = (await FirebaseFirestore.instance
        .collection(&quot;users&quot;)
        .doc(_uid)
        .collection(&quot;drug&quot;)
        .doc(drugID).collection(&quot;drugDates&quot;).where(&#39;time&#39;,isEqualTo: d).get()) as QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;;

huangapple
  • 本文由 发表于 2023年2月14日 00:10:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定