github.com/felangel/bl… github.com/huang12zhen…
requirement
- item change is sync, including navigation page.
- list refresh(no implement in sample) or loadMore would refresh items.
- close item in a right way.

- list refresh(no implement in sample) or loadMore would refresh items. I click right item (id=10) to 5, and scroll left to 10. Then, right item(id 10) to 0
core code
use dependeds to count dependeds.
class ItemBloc extends HydratedBloc<ItemEvent, ItemState> {
final String id;
// ItemBloc(this.id);
@override
Future<void> close() async{
dependeds[id]-=1;
debugPrint('close $id count: ${dependeds[id]}');
if (dependeds[id] == 0)
{
_cache.remove(id); /// check index
super.close();
}
}
factory ItemBloc(String id) {
if (_cache.containsKey(id)) {
return _cache[id];
} else {
final bloc = ItemBloc._(id);
_cache[id] = bloc;
dependeds[id] = 0;
return bloc;
}
}
static get cache => _cache;
static final Map<String, ItemBloc> _cache =
<String, ItemBloc>{};
static final Map<String,int> dependeds = <String,int> {};
ItemState get initialState => super.initialState ?? (ItemState(Item(id,0)));
ItemBloc._(this.id);
@override
Stream<ItemState> mapEventToState(
ItemEvent event,
) async* {
try {
yield* event.applyAsync(state, this);
} catch (_, stackTrace) {
developer.log('$_', name: 'ItemBloc', error: _, stackTrace: stackTrace);
yield state;
}
}
@override
ItemState fromJson(Map<String, dynamic> json) {
return ItemState(Item(json['id'],json['desc']));
}
@override
Map<String, dynamic> toJson(ItemState state) {
return {'id': state.item.id,'desc': state.item.desc};
}
onProvider(){
dependeds[id]+=1;
debugPrint('onProvider $id count: ${dependeds[id]}');
}
}