如何从Firebase中的选定文档中检索所有数据扑

时间:2020-08-22 05:02:10

标签: firebase flutter google-cloud-firestore

我需要检索与Firebase中特定文档相关的所有字段。该怎么办? 我在下面附加了我的代码。

enter image description here 我可以通过这种方式获取所有值

return new Text(snapshot.data['brand']);

但是我需要检索它们而不需要通过键调用。 有什么办法可以将所有数据检索到map或var中? 请帮我解决这个问题

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class ItemView extends StatefulWidget {
  final String docID123;

  const ItemView({ this.docID123}) ;

  @override
  _ItemViewState createState() => _ItemViewState();
}
String name123;
class _ItemViewState extends State<ItemView> {
  @override
  //String docuID = Widget.documentid;
  
  Widget build(BuildContext context) {
    //final  Map<String, Object>rcvdData = ModalRoute.of(context).settings.arguments;
    //docID = ${rcvdData['docuID'];}
    //String name123  = rcvdData[0];
    return Scaffold(
      appBar: AppBar(
        title: Text('Item Data'),
      ),
      body: ListView(
        children: <Widget>[
          Text(widget.docID123),
          getItems(),
        ],
      ),
    );
  }
}


Widget getItems(){
    return StreamBuilder(
      //1597917013710
      stream: Firestore.instance.collection('ads').document('1597917013710').snapshots(),
      builder: (context, snapshot){
        if (!snapshot.hasData) {
          //snapshot.data.toString();
          return new Text("Loading");
        }
        
        var userDocument = snapshot.data;
        String myname = snapshot.data.map.toString();
        //return new Text(snapshot.data['brand']);
        return new Text(myname);
      }
    );
}


Future getCatagory() async {
      var firestone = Firestore.instance;

      QuerySnapshot alldata = await firestone.collection("catagory_names/Vehicles").getDocuments();
      for(int i=0;i<alldata.documents.length;i++){
                        DocumentSnapshot snap = alldata.documents[i];
                        
                      }

      return  Text('12345');

    }

0 个答案:

没有答案