我正在根据连接状态在主列表中实现对我的应用程序的缓存。我的BLOC可以控制我的应用状态,但是当我的连接状态更改时,它不会更改。
这是我的UI部分中的代码:
Container(
color: Colors.white,
child: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: () async {
await Future.delayed(Duration(seconds: 1));
var connect = await bloc.checkConnection();
print('CONECT STATUS: $connect');
},
child: bloc.connectionChange
? BasicListControl(key: UniqueKey(), stream: bloc.headlines, widthSize: widthSize)
: BasicListControl(key: UniqueKey(), stream: bloc.cacheList, widthSize: widthSize),
)
),
这是我的StreamBuilder:
StreamBuilder<UnmodifiableListView<Headlines>>(
stream: stream,
builder: (context, snapshot) {
if (snapshot.data != null && snapshot.hasData) {
//print('VALUE LIST: ${snapshot.data.toString()}');
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
print('VALUE LIST: ${snapshot.data.toString()}');
return ItemHeadlinesList(
key: UniqueKey(), headlines: snapshot.data[index]);
});
}
return new OffLineBoard(widthSize: widthSize);
});
这是我的BLOC结构的一部分:
bool get connectionChange => _connectionChangeSubject.stream.value;
final _connectionChangeSubject = BehaviorSubject<bool>();
bool _hasConnection = false;
/// StatusConnection: isOnline, isOffline
Sink<StatusConnection> get statusConnection =>
_statusConnectionController.sink;
final _statusConnectionController = StreamController<StatusConnection>();
RadioBloc() {
_connectionChangeSubject.add(_hasConnection);
checkConnection().then((val) {
if (val) {
_setPlayerStateController.add(PlayerState.playing);
_connectionChangeSubject.add(_hasConnection);
_getHeadlinesHttp(rssVillaClara);
} else {
_setPlayerStateController.add(PlayerState.stopped);
_connectionChangeSubject.add(_hasConnection);
_getHeadlinesCache(_cache);
}
});
_statusConnectionController.stream.listen((value) {
if (value == StatusConnection.isOnline) {
_getHeadlinesHttp(rssVillaClara);
} else {
_getHeadlinesCache(_cache);
}
});
}
建立连接后,除了将缓存列表替换为在线连接列表之外,其他一切都很好,我注意到当我转到应用程序的另一个页面并返回时,如果更新了页面并且列表已更改,则返回。