从Flutter中的快照创建自己的流列表

时间:2020-08-18 09:47:47

标签: flutter dart google-cloud-firestore snapshot

背景: 我正在开发一个Flutter应用,该应用可以跟踪房租付款。我得到了一个列表,列出了租户尚未支付租金的月份(dueMonths),代码如下。

问题: 我受困的地方是,快照{}之后快照中的付款清单为空,但值在{}内部。如果Payments集合中有任何更改,则列表不会更新。

我想实现什么? 当用户付款时,我希望快照更新付款清单,然后更新notifyList(必须从该清单中删除月份)。我无法完成这项工作,并且浪费了过去两天的时间。请帮助我。

Stream<List> getNoPaymentList() {

StreamController<List> controller = new StreamController<List>();   //Add .broadcast here

// list of units rented by the tenant. He has rented 4A and G3
List units = [
  {'unitId': '4hFIcmbKhJgyrppsTBOc', 'unitName': '4A', 'propertyId': 'YF5k6D2abFsczVxSmLZu', 'rent': 6000},
  {'unitId': 'zDyCfrFYnsHIE2nAuP7B', 'unitName': 'G3', 'propertyId': 'IDroypXJyzMIb7a6jUqH', 'rent': 6000}
];
// checking the payments for the past three months
List dueMonths = ['2020-07', '2020-06', '2020-05'];
List payments = [];
List notifyList = [];

//document id of the user
String currentUid = 'kEamSzzDt4Yb9PgPGCg8AxDPrrp2';

//get list of payments done by the tenant for the due months
Stream<QuerySnapshot> paymentSS = Firestore.instance.collection('payment')
    .where('paid_for', whereIn: dueMonths)
    .where('tenant_id', isEqualTo: currentUid)
    .snapshots();
paymentSS.forEach((snapshot) {
  snapshot.documents.forEach((doc) {  //when there is a change in docs, this is working and payments list is added. notifyList is not updated.
    payments.add(
        {'unitId': doc.data['unit_id'], 'paidFor': doc.data['paid_for']}
    );
  });
});
// payments list is EMPTY outside the loop
units.forEach((unit) {
  dueMonths.forEach((month) {
    bool paid = false;
    payments.forEach((pay) {print('no pay');
      if(unit['unitId'] == pay['unitId'] && month == pay['paidFor']){
        paid = true;
      }
    });
    if(paid == false){
      notifyList.add({
        'unitId' : unit['unitId'],
        'unitName' : unit['unitName'],
        'propertyId' : unit['propertyId'],
        'rent' : unit['rent'],
        'notPaidFor' : month
      });
    }
  });
});

controller.sink.add(notifyList);
return controller.stream;

}

我正在使用以下代码收听流

                StreamBuilder(
                  stream: NotificationService().getNoPaymentList(),
                  builder: (context, snapshot) {
                    if(!snapshot.hasData){
                      return Text('Loading...');
                    }
                    var data = snapshot.data;
                    //print(data);
                    //print('status is'+ snapshot.connectionState.toString());
                    return Text('Stream Here');
                  },
                ),

0 个答案:

没有答案