Je veux accéder à tous les reg_events de l'utilisateur actuellement connecté. J'ai le code suivant en ce moment
builder(int index, DocumentSnapshot document) { return new AnimatedBuilder( animation: controller, builder: (context, child) { double value = 1.0; if (controller.position.haveDimensions) { value = controller.page - index; value = (1 - (value.abs() * .5)).clamp(0.0, 1.0); } return new Center( child: new SizedBox( height: Curves.easeOut.transform(value) * 200, width: Curves.easeOut.transform(value) * 1000, child: child, ), ); }, child: new Card( child: Text(document.data['reg_events'][0].toString(), textAlign: TextAlign.center, style: TextStyle(fontSize: 15, color: Colors.white),), margin: const EdgeInsets.all(10.0), color: index % 2 == 0 ? Colors.blue : Colors.red, ), ); }
Le 'constructeur' est:
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)), ), ); } ),
Mais il affiche "Instance de 'DocumentReference'". Comment accéder à cette référence de document?
3 Réponses :
Peut-être qu'il ne comprend pas reg_event en tant que liste, alors essayez ceci,
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)),} ), ); } ),
DocumentReference est comme un pointeur vers un document. Vous pouvez obtenir le document unique en utilisant la méthode .get
qui renvoie Future<DocumentSnapshot>
. Puisque vous en avez un éventail, vous pouvez ensuite utiliser Streams pour obtenir un tas de Futures.
Firestore.instance.collection('users').document("$email/reg_events").snapshots();
Mais...
Firestore a des requêtes, donc ça devrait être mieux si vous l'utilisez réellement. Vous devriez pouvoir référencer vos reg_events
comme ça:
List<DocumentReference> references = [ref, ref, ref]; var myStream = Stream.fromFutures(references.map((ref) => ref.get()).toList()); StreamBuilder(builder: ..., stream: myStream);
Dans cet exemple, un objet User est créé qui contient une liste de références des entités (ou événements). Cette liste est ensuite transmise à la classe DatabaseService qui renvoie une liste d'objets de flux EntityDetails.
DatabaseService Classe:
class UserDetails { final String email; final String phone; final String fname; final String lname; final String streetNr; final String city; final List<DocumentReference> entities; UserDetails( {this.email, this.phone, this.fname, this.lname, this.streetNr, this.city, this.entities}); }
Widget
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"); }); });
Objet utilisateur
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'], );}
Pourriez-vous inclure votre constructeur?
J'ai mis à jour mon message maintenant @ pr0gramist