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

StreamBuilder's snapshot doesn't have data despite sending it through Stream

TL;DR:添加到 StreamSink 的值出于某种未知原因被受人尊敬的 StreamBuilder 的 initialValue

覆盖

从表面上看,我的问题类似于 Github 上的 this 问题,但唯一的区别是我在控制台日志中看到了两条语句,而不是获取单个值,具有正确值的一个被添加到流中,紧随其后的是传递给流构建器的 initialValue。

就我而言,我使用的是 generic_bloc_provider


详细说明

我有一个 bloc 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(第 145 行)内的 streamBuilders 中使用。问题是,即使在将数据添加到相应的接收器(在本例中为 _totalDebitSink)之后,这些值也不会在 Dashboard class 内的流构建器中更新。为了进一步调查,我在 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 的初始值.

我在 dashboardBloc.dart(第 88 行)和 Dashboard.dart(第 212 行)中使用了类似的技术,效果非常好。

到目前为止我还没有找到任何解决方案。

终于找到解决办法了。事实证明这是我的一个愚蠢的错误。 我不小心将错误的流和接收器映射到错误的控制器。 在这种情况下,

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

在这里我不小心将 totalCredtiStream 映射到 debitController 的流。

另外,感谢@pskink 指出。