简体   繁体   English

无法从 Cloud firestore 中检索 Flutter 中的多个集合

[英]Cant Retrieve more than one collection in Flutter from cloud firestore

Hy Guys what I am trying to do is to retrieve 2 collection documents from Firestore database and display into my flutter my code is:伙计们,我想做的是从 Firestore 数据库中检索 2 个集合文档并显示到我的 flutter 中,我的代码是:

Future<QuerySnapshot> getData() async {
    var firebaseUser = await FirebaseAuth.instance.currentUser();
    return await Firestore.instance
        .collection("user")
        .where("email", isEqualTo: firebaseUser.email)
        .getDocuments();
  }

  @override
  Widget build(BuildContext context) {
    // Build a Form widget using the _formKey created above.
    return FutureBuilder(
      future: getData(),

that allow me to retrieve data from a specific user but I have another form called "shippingAddress" which create collection as below:这允许我从特定用户检索数据,但我有另一个名为“shippingAddress”的表单,它创建如下集合:

void createRecord() async {
    await databaseReference.collection("shippingAddress").add({
      'alias': '$alias',
      'shippingName': '$shippingName',
      'shippinglastName': '$shippinglastName',
      'street': '$street',
      'streetNumber': '$streetNumber',
      'zipNumber': '$zipNumber',
      'phoneNumber': '$phoneNumber',
      'textMessage': '$textMessage',
      'totalQuantity': '$totalQuantity',
      'totalWeight': '$totalWeight',
    }).then((value) {
      print(value.documentID);
    });
  }

then I have to retrieve also the above collection but I dont know how... because in the same screen I retrieve "user" collection to display only data from a specific user and I need also to retrieve "shippingAddress" collection because in such collection there are other data to be displayed for example If I retrieve然后我还必须检索上述集合,但我不知道如何...因为在同一屏幕中我检索“用户”集合以仅显示来自特定用户的数据,我还需要检索“shippingAddress”集合,因为在此类集合中还有其他数据要显示,例如如果我检索

${snapshot.data.documents[index].data["email"]} or ${snapshot.data.documents[index].data["name"]}

that I have got into signup form it works and into my widget it show the email or name but if I want for example retrieve:我已经进入注册表单,它可以工作,并且在我的小部件中显示 email 或名称,但如果我想要检索:

${snapshot.data.documents[index].data["alias"]}

that is create into "shippingAddress" collection I cant get them这是创建到“shippingAddress”集合我无法得到它们

I have got myself the solution.我已经找到了解决方案。 First of all getData from existing user:首先从现有用户那里获取数据:

final databaseReference = Firestore.instance;

  Future<QuerySnapshot> getData() async {
    var firebaseUser = await FirebaseAuth.instance.currentUser();
    return await Firestore.instance
        .collection("user")
        .where("email", isEqualTo: firebaseUser.email)
        .getDocuments();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: getData(),
      builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return ListView.builder(
            itemCount: snapshot.data.documents.length,
              itemBuilder: (BuildContext context, int index) {
                return Column(
                    children: <Widget>[
                    Stream(),

and call a Stream Builder with other collection:并使用其他集合调用 Stream Builder:

lass Stream extends StatefulWidget {
  @override
  _StreamState createState() => _StreamState();
}

class _StreamState extends State<Stream> {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: Firestore.instance.collection('shippingAddress').snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData)
          return Text('Loading data please Wait');
        return Column(
          children: <Widget>[
            Container(
              height: 1000,
              child: ListView.builder(
                itemCount: snapshot.data.documents.length,

thats all!就这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在云 Firestore 集合中添加多个文档 - Can't add more than one document in the cloud firestore collection 有没有办法将多个云 function 附加到 Firestore 中的特定集合? - Is there a way to attach more than one cloud function to a specific collection in Firestore? 如何使用 flutter 和 dart 从 Cloud Firestore 检索特定数据? - how to retrieve specific data from Cloud Firestore using flutter and dart? 使用 Flutter 在一次往返中从 firestore 获取超过 10 个文档 - get more than 10 documents from firestore in one round trip using Flutter 如何使用 Flutter 检索我的 Cloud Firestore 集合中最后添加的文档? - How to retrieve the last added document in my Cloud Firestore collection, using Flutter? 无法从 flutter Cloud firestore 获取一份文件 - Can't get one document from flutter cloud firestore 如何使用 Flutter 和 Future Builder 从 Cloud Firestore 正确检索数据? - How to properly retrieve data from Cloud Firestore using Flutter and Future Builder? Cloud Firestore Firebase Collection ref 上的 IN 查询是否有 Flutter/Dart 等价物? - Is there a Flutter/Dart equivalent for the IN query on a Cloud Firestore Firebase Collection ref? 如何在 Flutter 中从 Firestore 存储和检索颜色 - How to store and retrieve Color from Firestore in Flutter Flutter - 无法从 firestore 获取集合 - Flutter - Can't get collection from firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM