我正在处理一个有多个用户的应用程序.我有一个资源列表,这个资源列表就像一个巧克力列表(只有一个并且是唯一的).现在,我在所有活动用户的主屏幕上显示此巧克力.现在,用户可以单击巧克力,它将被提供给他们.但是,当发生这种情况时,我想刷新所有已登录的用户,以确保没有两个用户使用相同的巧克力.
I am working on one application which has multiple users. I have a list of resources, this list of resources are like a list of chocolates (only one and unique). Now, I am showing this chocolates on the home screen of all active users. Now, user can click on chocolate and it will be given to them. But, when this happens i want to refresh all logged in users so to ensure that no two users are having same chocolate.
我正在使用数据库触发器来监视数据库中的更改.我能够做到这一点,但我关心的是如何刷新listView.
I am using database trigger to monitor the change in DB. I am able to do that but my concern is how to refresh listView.
我的算法如下:
1)监视数据库中的更改.2)获取新的数据集.3)更新视图
1) Monitor changes in Database. 2) Get Fresh set of data. 3) Update View
我尝试如下创建syncDatabaseFunction:
I tried creating syncDatabaseFunction as below:
Future syncDatabaseFunction() async { CollectionReference reference = Firestore.instance.collection('Chocolates'); reference.snapshots().listen((querySnapshot){ querySnapshot.documentChanges.forEach((change){ print("Changed Chocolate"); BackendOperations.getAllChocolates().then((value){ var chocolateTemp = (value as List<ChocolateModel>) .where((element) => (element.chocolateColor == "Brown")) .toList(); print("Count is "); return chocolateTemp; }); }); }); }对于列表视图,我正在使用futureBuilder.
For listview I am using futureBuilder.
推荐答案我认为,如果您使用 StreamBuilder ,则可以解决问题.当用户删除或添加新评论时,它将向所有用户显示. StreamBuilder 就是这样做的,是Stream的观察者.
I think that if you use StreamBuilder you will solve the problem. When a user remove or add a new Comment it show for all users. StreamBuilder was made to do this, be a Observer of the Stream.
这是我的代码:
Widget getListComment() { return StreamBuilder<QuerySnapshot>( stream: Firestore.instance .collection('comments') .where('post', isEqualTo: postRef) .orderBy('createdAt', descending: true) .snapshots(), builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { if (snapshot.hasError) return new Text('Error: ${snapshot.error}'); switch (snapshot.connectionState) { case ConnectionState.waiting: return Column( children: <Widget>[ CircularProgressIndicator(), ], ); default: return new ListView( children: snapshot.data.documents.map((DocumentSnapshot document) { return CommentItem( key: Key(document.documentID), comment: Comment.fromDoc(document), myUser: widget.myUser, ); }).toList(), ); } }, ); }我收到Firebase的评论,并显示在 ListView 中,我认为这就像您的巧克力.
I receive comments from Firebase and show in a ListView, I think that is like your chocolates.
更多推荐
使用Flutter的多用户应用程序
发布评论