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



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

覆盖从表面上看,我的问题类似于 Github 上的这个问题,但唯一的区别是,我没有获得单个值,而是在控制台日志上看到两个语句,一个具有添加到流中的正确值,紧跟传递给流构建器的 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}nn");
return Text("₹${snapshot.data.floor()}",
style: _textStyleFooter);
},
),

然后,这些接收器稍后在 Dashboard.dart(第 145 行(中的 streamBuilders 中使用。问题是,即使在将数据添加到相应的接收器(在本例中为_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是提供给流构建器的初始值,也分配给变量_totalDebit_totalCredit

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

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

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

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

在这里,我不小心将totalCredtiStreamdebitController的流映射在一起。

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

最新更新