Simple Refresh Bus

A lightweight event bus for cross-cubit/bloc state synchronization in Flutter BLoC applications.

Installation

dependencies:
  simple_refresh_bus: ^1.0.0

Usage

Subscribe to refresh signals (Cubit)

class ProfileGetCubit extends Cubit<ProfileGetState>
    with RefreshBusSubscriber<ProfileGetState> {

  ProfileGetCubit() : super(const ProfileGetState()) {
    onRefresh<Profile>(load);  // Reload when Profile signal received
  }

  Future<void> load() async {
    // Fetch from API...
  }
}

Subscribe to refresh signals (Bloc)

class ProfileBloc extends Bloc<ProfileEvent, ProfileState>
    with RefreshBusSubscriber<ProfileState> {

  ProfileBloc() : super(const ProfileState()) {
    on<ProfileLoadRequested>(_onLoadRequested);
    onRefresh<Profile>(() async => add(ProfileLoadRequested()));
  }
}

Emit refresh signals

// Trigger reload on all listeners
RefreshBus.instance.refresh<Profile>();

// Or pass data directly (listeners receive it via onData)
RefreshBus.instance.push<Profile>(updatedProfile);

Listen for data

onData<Profile>((profile) => emit(state.copyWith(profile: profile)));

Testing

Override refreshBus to use an isolated instance:

class TestableProfileCubit extends ProfileGetCubit {
  final RefreshBus _testBus;
  TestableProfileCubit(this._testBus);

  @override
  RefreshBus get refreshBus => _testBus;
}

// In tests
final testBus = RefreshBus.custom();
final cubit = TestableProfileCubit(testBus);
testBus.refresh<Profile>();  // Triggers cubit reload

License

MIT

Libraries

simple_refresh_bus