尽管通过Stream发送,但StreamBuilder的快照没有数据

时间:2019-12-25 19:58:45

标签: flutter dart google-cloud-firestore stream bloc

TL; DR:添加到StreamSink的值由于某些未知原因而被尊敬的StreamBuilder的initialValue覆盖

从外观上看,我的问题类似于Github上的this问题,但是唯一的不同是,我没有在控制台日志中看到两个语句,而是得到了一个正确的值,而不是得到一个值。被添加到流中,紧随其后的是InitialValue,该initialValue传递给了流生成器。

就我而言,我正在使用generic_bloc_provider


详细说明

我有一个udhariBloc.dart组(第99行),它侦听集合引用并将该值添加到某些接收器。

void _fetchUdhari() {
    Firestore.instance
        .collection("Users 3.0")
        .document("data")
        .collection("udhari")
        .where("participants", arrayContains: userBloc.phoneNumber)
        .where("firstPartyDeleted", isEqualTo: false)
        .snapshots()
        .listen((QuerySnapshot snapshot) {

//Initialized these to zero because their
//value must be recalculated upon every change.
//These variables were initialized to zero(their default value) when declared.
      _udhariList = List<UdhariClass>();
      _totalDebit = 0;
      _totalCredit = 0;

      for (int i = 0; i < snapshot.documents.length; i++) {
        _udhariList.add(UdhariClass.fromSnapshot(
          snapshot: snapshot.documents[i],
          userBloc: userBloc,
        ));
      }

      // Remove the udhari records where the participant is secondParty
      // and secondParty has already deleted the record
      _udhariList.removeWhere((UdhariClass udhari) {
        return ((udhari.partyType == PartyType.SecondParty) &&
            (udhari.secondPartyDeleted == true));
      });

      for (int i = 0; i < _udhariList.length; i++) {
        if (_udhariList[i].udhariType == Udhari.Borrowed) {
          _totalDebit += _udhariList[i].amount;
        } else {
          _totalCredit += _udhariList[i].amount;
        }
      }
//The correct values are calculated correctly, 
//printed and added to respective sinks as well
      print("Debit: $_totalDebit");
      print("Credit: $_totalCredit");
      print("List: ${_udhariList[0].context}");
      _totalDebitSink.add(_totalDebit);
      _totalCreditSink.add(_totalCredit);
      _udhariListSink.add(_udhariList);
    });
  }

这是流及其控制器

/// Stream controller for displaying total debit on dashboard
  StreamController<double> _totalDebitController =
      StreamController<double>.broadcast();
  Stream<double> get totalDebitStream => _totalDebitController.stream;
  StreamSink<double> get _totalDebitSink => _totalDebitController.sink;

/// Stream controller for displaying list of udhari
  StreamController<List<UdhariClass>> _udhariListController =
      StreamController<List<UdhariClass>>.broadcast();
  Stream<List<UdhariClass>> get udhariListStream =>
      _udhariListController.stream;
  StreamSink<List<UdhariClass>> get _udhariListSink =>
      _udhariListController.sink;

/// Stream controller for displaying total credit on dashboard
  StreamController<double> _totalCreditController =
      StreamController<double>.broadcast();
  Stream<double> get totalCreditStream => _totalDebitController.stream;
  StreamSink<double> get _totalCreditSink => _totalDebitController.sink;

这是我的流生成器​​,正在使用上述流

 StreamBuilder<double>(
   initialData: udhariBloc.getTotalDebit,
   stream: udhariBloc.totalDebitStream,
   builder: (BuildContext context,
       AsyncSnapshot<double> snapshot) {
     print(
         "============INSIDE DEBIT STREAM BLDR============");
     print("Conn State: ${snapshot.connectionState}");
     print("Has Data: ${snapshot.hasData}");
     print("Data: ${snapshot.data}");
     print("Has Error: ${snapshot.hasError}");
     print("Error: ${snapshot.error}\n\n");
     return Text("₹${snapshot.data.floor()}",
         style: _textStyleFooter);
   },
 ),

这些接收器随后在Dashboard.dart内的streamBuilders中消耗(第145行)。问题在于,即使在将数据添加到各个接收器(在这种情况下为_totalDebitSink)中之后,这些值也不会在Dashboard类内的流生成器​​中更新。为了进一步调查,我在UdhariBloc的构造函数内的_totalDebitStream上附加了一个侦听器

totalDebitStream.listen((onData) {
      print("CURRENT DEBIT: $onData");
    }, onError: (error) {
      print("Error listening Debit :$error");
    }, onDone: () {
      print("Done listening to Debit values");
    });

每次更改值时,我都会在控制台中看到此日志。

CURRENT DEBIT: 100
CURRENT DEBIT: 0
============INSIDE DEBIT STREAM BLDR============
Conn State: ConnectionState.active
Has Data: true
Data: 0.0
Has Error: false
Error: null

在这里,100是Firestore的更新值,0是初始值,该初始值提供给StreamBuilder,并且还分配给变量_totalDebit_totalCredit。 / p>

我在dashboardBloc.dart(第88行)和Dashboard.dart(第212行)中使用了类似的技术,它就像是一种魅力。

到目前为止,我还找不到任何解决方案。

1 个答案:

答案 0 :(得分:0)

最后,找到了解决方案。原来这是我的愚蠢错误。 我不小心将错误的流映射到了错误的控制器。 在这种情况下,

Stream<double> get totalCreditStream => _totalDebitController.stream;
StreamSink<double> get _totalCreditSink => _totalDebitController.sink;

在这里,我不小心将totalCredtiStreamdebitController的流进行了映射。

此外,感谢@pskink指出了这一点。

相关问题