import 'dart:async'; import 'package:bloc/bloc.dart'; class TransformerCubit extends Cubit { TransformerCubit(this.input, {required this.transform}) : super(transform(input.state)) { _subscription = input.stream.listen((event) => emit(transform(event))); } @override Future close() async { await _subscription.cancel(); await input.close(); await super.close(); } Cubit input; T Function(S) transform; late final StreamSubscription _subscription; }