Stream <QuerySnapshot>返回空文档

时间:2020-07-03 14:06:25

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

我对抖动和云存储有疑问。我的数据库连接正常,可以创建新条目,但是我的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();
}

谢谢!

1 个答案:

答案 0 :(得分: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'];
        
        });