如何在flutter中访问DocumentReference数组?

时间:2019-12-11 08:57:09

标签: android firebase flutter google-cloud-firestore

the reg_events field is an array of references to the events collection.

我想访问当前登录用户的所有reg_events。我现在有以下代码

    var it = 0

    val uploadImage=AndroidNetworking.upload(Constant.BASE_URL + "app.php?api=editprofile")
    repeat(imagesFiles.size){
        uploadImage.addMultipartFile("image", imageFileOne)
        uploadImage.addMultipartFile("image${it+1}", imagesFiles[it])

    }
    it++

“构建者”是:

       stream: Firestore.instance.collection('users').document(email).snapshots(),
       builder: (context, snapshot){
         if(!snapshot.hasData){
           return Text("Loading..");
         }
         return Center(
          child: new Container(
            child: new PageView.builder(
                onPageChanged: (value) {
                  setState(() {
                    currentpage = value;
                  });
                },
                controller: controller,
                itemCount: snapshot.data['reg_events'].length,
                itemBuilder: (context, index) => builder(index, snapshot.data)),
          ),
        );
       }
     ),

但是它输出“'DocumentReference'的实例”。如何访问此文档参考?

3 个答案:

答案 0 :(得分:0)

也许它不理解 reg_event 为列表,所以尝试一下,

 stream: Firestore.instance.collection('users').document(email).snapshots(),
  builder: (context, snapshot){
     List regEvent = new List();
     if(snapshot.hasData){
        regEvent = snapshot.data['reg_events'];
      }
      if(!snapshot.hasData){
        return Text("Loading..");
      }
      return Center(
           child: new Container(
                 child: new PageView.builder(
                         onPageChanged: (value) {
                                setState(() {
                                      currentpage = value;
                                });
                          },
                         controller: controller,
                         itemCount: regEvent.length,
                         itemBuilder: (context, index) { 
                               print(regEvent[index]);
                               return builder(index, snapshot.data)),}
                      ),
               );
         }
),

答案 1 :(得分:0)

DocumentReference就像一个指向一个文档的指针。您可以使用.get方法获得单个文档,该方法返回Future<DocumentSnapshot>。既然有了它们的数组,那么您就可以使用Streams获得一堆期货。

List<DocumentReference> references = [ref, ref, ref];

var myStream = Stream.fromFutures(references.map((ref) => ref.get()).toList());

StreamBuilder(builder: ..., stream: myStream);

但是...

Firestore具有查询功能,因此如果您实际使用它会更好。您应该能够像这样引用您的reg_events

Firestore.instance.collection('users').document("$email/reg_events").snapshots();

答案 2 :(得分:0)

在此示例中,创建了一个User对象,其中包含实体(或事件)的引用列表。然后,此列表传递给DatabaseService类,该类返回EntityDetails流对象的列表。

DatabaseService类别:

# /etc/hosts file
127.0.0.1 localhost

YOUR_SERVER_IP project1.com
YOUR_SERVER_IP project2.com
YOUR_SERVER_IP project3.com

小部件

final CollectionReference entityCollection =
      Firestore.instance.collection('entities');
  final CollectionReference userCollection =
      Firestore.instance.collection('user');

 Stream<UserDetails> get userDetails {
    return userCollection
        .document(uid)
        .snapshots()
        .map(_userDetailsFromSnapshot);
  }

UserDetails _userDetailsFromSnapshot(DocumentSnapshot snapshot) {
    return UserDetails(
        email: snapshot.data['email'],
        phone: snapshot.data['phone'],
        fname: snapshot.data['fname'],
        lname: snapshot.data['lname'],
        streetNr: snapshot.data['street_nr'],
        city: snapshot.data['city'],
        entities: List.from(snapshot.data['entities']));
  }

 List<Stream<EntityDetails>> getEntitiesFromDRList(
      List<DocumentReference> entities) {
    List<Stream<EntityDetails>> elist = new List();
    entities.forEach((element) {
      elist.add(element.snapshots().map((_entityDetailsFromSnapshot)));
    });
    return elist;
  }

EntityDetails _entityDetailsFromSnapshot(DocumentSnapshot snapshot) {
return EntityDetails(
  uid: uid,
  name: snapshot.data['name'],
  description: snapshot.data['description'],
  type: snapshot.data['type'],
  geoPoint: snapshot.data['geopoint'],
  adressString: snapshot.data['adress_string'],
  email: snapshot.data['email'],
  phone: snapshot.data['phone'],
);}

用户对象

stream: DatabaseService(uid: uid).userDetails,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            UserDetails userDetails = snapshot.data;
            //Get the Streams
            DatabaseService(uid: uid)
                .getEntitiesFromDRList(userDetails.entities)
                .forEach((element) {
              element.listen((data) {
                print("DataReceived: " + data.name);
              }, onDone: () {
                print("Task Done");
              }, onError: (error) {
                print("Some Error");
              });
            });