【问题标题】:Stream<QuerySnapshot> returns null documentsStream<QuerySnapshot> 返回空文档
【发布时间】:2020-10-24 06:40:28
【问题描述】:

我对 Flutter 和 cloud-firestore 有疑问。我的数据库连接正常,我可以创建新条目,但我的 Stream 返回空文档。

我的 database.dart 类:

final CollectionReference idCollection = Firestore.instance.collection("idCollection");

Future addId(String idName) async {
    return await idCollection
        .document(uid)
        .collection("userIds")
        .document(idName)
        .setData({
      'key': uid + _randomString(6),
    });
}

Stream<QuerySnapshot> get ids {
   return idCollection.snapshots();
}

实际上我希望快照属于以下内容,但由于顶部集合已经抛出 null 错误,我不确定问题出在哪里。

 Stream<QuerySnapshot> get ids {
   return idCollection
        .document(uid)
        .collection("userIds").snapshots();
}

包装提供者的类

    return StreamProvider<QuerySnapshot>.value(
      value: DatabaseService().ids,

错误和引发错误的类:

════════ Exception caught by widgets library ═══════════════════════════════════
The getter 'documents' was called on null.
Receiver: null
Tried calling: documents
The relevant error-causing widget was
    IdList 
lib\…\home\home.dart:39
════════════════════════════════════════════════════════════════════════════════
class _IdListState extends State<IdList> {
  @override
  Widget build(BuildContext context) {
    final ids = Provider.of<QuerySnapshot>(context);

    for (var doc in ids.documents) {
      print(doc.data);
    }

    return Scaffold(
      body: Center(
        child: Text("sdf"),
      ),
    );
  }
}

class IdList extends StatefulWidget {
  @override
  State<IdList> createState() => _IdListState();
}

提前致谢!

【问题讨论】:

  • 如果为 null 表示您的收藏中没有数据。按预期工作

标签: firebase flutter dart google-cloud-firestore flutter-provider


【解决方案1】:

这是使用 querysnapshot 查询数据库的方法

    Future _getUsers() async {
     QuerySnapshot querySnapshot = await Firestore.instance
            .collection('idCollection')
            //you may want to sort your data here 
           
            .getDocuments();
        print('###########s result ${querySnapshot.documents}');

  if (querySnapshot != null) {
      for (int i = 0; i < querySnapshot.documents.length; i++) {
        var a = querySnapshot.documents[i];
        print(a.documentID);
 setState(() {
          documentId = a.documentID;
         userID = a.data['userId'];
        
        });

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    相关资源
    最近更新 更多